Skip to content

Commit 3739284

Browse files
authored
Merge pull request #1 from ioncakephper/chore/add-core-functions
2 parents 4059629 + ef4f490 commit 3739284

File tree

8 files changed

+413
-1
lines changed

8 files changed

+413
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Changelog
2+
3+
*Changelog created using the [Simple Changelog](https://marketplace.visualstudio.com/items?itemName=tobiaswaelde.vscode-simple-changelog) extension for VS Code.*

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return require('./src/source-processor');

jest.config.js

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/**
2+
* For a detailed explanation regarding each configuration property, visit:
3+
* https://jestjs.io/docs/configuration
4+
*/
5+
6+
/** @type {import('jest').Config} */
7+
const config = {
8+
// All imported modules in your tests should be mocked automatically
9+
// automock: false,
10+
11+
// Stop running tests after `n` failures
12+
// bail: 0,
13+
14+
// The directory where Jest should store its cached dependency information
15+
// cacheDirectory: "C:\\Users\\ig343\\AppData\\Local\\Temp\\jest",
16+
17+
// Automatically clear mock calls, instances, contexts and results before every test
18+
// clearMocks: false,
19+
20+
// Indicates whether the coverage information should be collected while executing the test
21+
// collectCoverage: false,
22+
23+
// An array of glob patterns indicating a set of files for which coverage information should be collected
24+
// collectCoverageFrom: undefined,
25+
26+
// The directory where Jest should output its coverage files
27+
// coverageDirectory: undefined,
28+
29+
// An array of regexp pattern strings used to skip coverage collection
30+
// coveragePathIgnorePatterns: [
31+
// "\\\\node_modules\\\\"
32+
// ],
33+
34+
// Indicates which provider should be used to instrument code for coverage
35+
coverageProvider: "v8",
36+
37+
// A list of reporter names that Jest uses when writing coverage reports
38+
// coverageReporters: [
39+
// "json",
40+
// "text",
41+
// "lcov",
42+
// "clover"
43+
// ],
44+
45+
// An object that configures minimum threshold enforcement for coverage results
46+
// coverageThreshold: undefined,
47+
48+
// A path to a custom dependency extractor
49+
// dependencyExtractor: undefined,
50+
51+
// Make calling deprecated APIs throw helpful error messages
52+
// errorOnDeprecated: false,
53+
54+
// The default configuration for fake timers
55+
// fakeTimers: {
56+
// "enableGlobally": false
57+
// },
58+
59+
// Force coverage collection from ignored files using an array of glob patterns
60+
// forceCoverageMatch: [],
61+
62+
// A path to a module which exports an async function that is triggered once before all test suites
63+
// globalSetup: undefined,
64+
65+
// A path to a module which exports an async function that is triggered once after all test suites
66+
// globalTeardown: undefined,
67+
68+
// A set of global variables that need to be available in all test environments
69+
// globals: {},
70+
71+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
72+
// maxWorkers: "50%",
73+
74+
// An array of directory names to be searched recursively up from the requiring module's location
75+
// moduleDirectories: [
76+
// "node_modules"
77+
// ],
78+
79+
// An array of file extensions your modules use
80+
// moduleFileExtensions: [
81+
// "js",
82+
// "mjs",
83+
// "cjs",
84+
// "jsx",
85+
// "ts",
86+
// "tsx",
87+
// "json",
88+
// "node"
89+
// ],
90+
91+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
92+
// moduleNameMapper: {},
93+
94+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
95+
// modulePathIgnorePatterns: [],
96+
97+
// Activates notifications for test results
98+
// notify: false,
99+
100+
// An enum that specifies notification mode. Requires { notify: true }
101+
// notifyMode: "failure-change",
102+
103+
// A preset that is used as a base for Jest's configuration
104+
// preset: undefined,
105+
106+
// Run tests from one or more projects
107+
// projects: undefined,
108+
109+
// Use this configuration option to add custom reporters to Jest
110+
// reporters: undefined,
111+
112+
// Automatically reset mock state before every test
113+
// resetMocks: false,
114+
115+
// Reset the module registry before running each individual test
116+
// resetModules: false,
117+
118+
// A path to a custom resolver
119+
// resolver: undefined,
120+
121+
// Automatically restore mock state and implementation before every test
122+
// restoreMocks: false,
123+
124+
// The root directory that Jest should scan for tests and modules within
125+
// rootDir: undefined,
126+
127+
// A list of paths to directories that Jest should use to search for files in
128+
// roots: [
129+
// "<rootDir>"
130+
// ],
131+
132+
// Allows you to use a custom runner instead of Jest's default test runner
133+
// runner: "jest-runner",
134+
135+
// The paths to modules that run some code to configure or set up the testing environment before each test
136+
// setupFiles: [],
137+
138+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
139+
// setupFilesAfterEnv: [],
140+
141+
// The number of seconds after which a test is considered as slow and reported as such in the results.
142+
// slowTestThreshold: 5,
143+
144+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
145+
// snapshotSerializers: [],
146+
147+
// The test environment that will be used for testing
148+
// testEnvironment: "jest-environment-node",
149+
150+
// Options that will be passed to the testEnvironment
151+
// testEnvironmentOptions: {},
152+
153+
// Adds a location field to test results
154+
// testLocationInResults: false,
155+
156+
// The glob patterns Jest uses to detect test files
157+
// testMatch: [
158+
// "**/__tests__/**/*.[jt]s?(x)",
159+
// "**/?(*.)+(spec|test).[tj]s?(x)"
160+
// ],
161+
162+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
163+
// testPathIgnorePatterns: [
164+
// "\\\\node_modules\\\\"
165+
// ],
166+
167+
// The regexp pattern or array of patterns that Jest uses to detect test files
168+
// testRegex: [],
169+
170+
// This option allows the use of a custom results processor
171+
// testResultsProcessor: undefined,
172+
173+
// This option allows use of a custom test runner
174+
// testRunner: "jest-circus/runner",
175+
176+
// A map from regular expressions to paths to transformers
177+
// transform: undefined,
178+
179+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
180+
// transformIgnorePatterns: [
181+
// "\\\\node_modules\\\\",
182+
// "\\.pnp\\.[^\\\\]+$"
183+
// ],
184+
185+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
186+
// unmockedModulePathPatterns: undefined,
187+
188+
// Indicates whether each individual test should be reported during the run
189+
// verbose: undefined,
190+
191+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
192+
// watchPathIgnorePatterns: [],
193+
194+
// Whether to use watchman for file crawling
195+
// watchman: true,
196+
};
197+
198+
module.exports = config;

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
},
2525
"homepage": "https://github.com/ioncakephper/insert-file-tag#readme",
2626
"scripts": {
27-
"test": "jest --passWithNoTests"
27+
"test": "jest"
2828
},
2929
"dependencies": {
30+
"fs": "^0.0.1-security",
3031
"fs-extra": "^11.1.1"
3132
},
3233
"devDependencies": {

src/processFilename.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { processFilename } = require('./source-processor');
4+
5+
describe('processFilename', () => {
6+
it('throws an error when no filename is provided', () => {
7+
expect(() => processFilename()).toThrow(Error);
8+
});
9+
10+
it('throws a TypeError when the filename is not a string', () => {
11+
expect(() => processFilename(123)).toThrow(TypeError);
12+
});
13+
14+
it('returns null when the file does not exist', () => {
15+
const filename = 'non-existent-file.txt';
16+
expect(processFilename(filename)).toBeNull();
17+
});
18+
19+
it('returns null when the file is empty', () => {
20+
const filename = 'empty-file.txt';
21+
fs.writeFileSync(filename, '');
22+
expect(processFilename(filename)).toBeNull();
23+
fs.unlinkSync(filename);
24+
});
25+
26+
it('processes the markdown content correctly when the file exists and is not empty', () => {
27+
const filename = 'test-file.txt';
28+
const fileContent = 'This is a test file.';
29+
fs.writeFileSync(filename, fileContent);
30+
const result = processFilename(filename);
31+
expect(result).toContain(fileContent);
32+
fs.unlinkSync(filename);
33+
});
34+
35+
// it('throws an error when an error occurs while reading the file', () => {
36+
// const filename = 'test-file.txt';
37+
// fs.chmodSync(filename, 0o000); // make the file unreadable
38+
// expect(() => processFilename(filename)).toThrow(Error);
39+
// fs.chmodSync(filename, 0o644); // restore the file permissions
40+
// });
41+
});

src/processSourceCode.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const {processSourceCode} = require('./source-processor');
4+
5+
describe('processSourceCode', () => {
6+
it('throws TypeError when sourceCode is not a string', () => {
7+
expect(() => processSourceCode(123)).toThrow(TypeError);
8+
});
9+
10+
it('replaces insert/include tags with file content', () => {
11+
const sourceCode = '<!-- ::insert file="test.txt" --><!-- :/insert -->';
12+
const basePath = './test';
13+
const fileContent = 'Hello World!';
14+
const filename = path.join(basePath, 'test.txt');
15+
fs.mkdirSync(path.dirname(filename), { recursive: true });
16+
fs.writeFileSync(path.join(basePath, 'test.txt'), fileContent, 'utf8');
17+
const result = processSourceCode(sourceCode, basePath);
18+
expect(result).toContain(fileContent);
19+
fs.unlinkSync(path.join(basePath, 'test.txt'));
20+
});
21+
22+
// it('handles missing file paths in tags', () => {
23+
// const sourceCode = '<!-- ::insert --><!-- :/insert -->';
24+
// const basePath = './test';
25+
// fs.mkdirSync(path.join(basePath), { recursive: true });
26+
// const result = processSourceCode(sourceCode, basePath);
27+
// expect(result).toContain('<!-- Error: missing file path -->');
28+
// });
29+
30+
it('handles errors when reading files', () => {
31+
const sourceCode = '<!-- ::insert file="non-existent.txt" --><!-- :/insert -->';
32+
const basePath = './test';
33+
const result = processSourceCode(sourceCode, basePath);
34+
expect(result).toContain('<!-- Error processing file: non-existent.txt -->');
35+
});
36+
37+
it('resolves file paths relative to the provided base path', () => {
38+
const sourceCode = '<!-- ::insert file="test.txt" --><!-- :/insert -->';
39+
const basePath = './test/subdir';
40+
const fileContent = 'Hello World!';
41+
fs.writeFileSync(path.join(basePath, 'test.txt'), fileContent);
42+
const result = processSourceCode(sourceCode, basePath);
43+
expect(result).toContain(fileContent);
44+
fs.unlinkSync(path.join(basePath, 'test.txt'));
45+
});
46+
});

0 commit comments

Comments
 (0)