-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yoann Chaudet
committed
Jul 19, 2022
1 parent
250c4fb
commit 931e155
Showing
8 changed files
with
206 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| const fs = require('fs') | ||
|
|
||
| const {ConfigParser} = require('./config-parser') | ||
| const {getTempFolder, compareFiles} = require('./test-helpers') | ||
|
|
||
| // Get the temp folder | ||
| const tempFolder = getTempFolder() | ||
|
|
||
| // Cases to test | ||
| const cases = [ | ||
| // | ||
| // Default export | ||
| // | ||
|
|
||
| { | ||
| property: 'property', | ||
| source: `export default {}`, | ||
| expected: `export default { property: "value" }` | ||
| }, | ||
| { | ||
| property: 'property', | ||
| source: `export default { property: 0 }`, // property exists and is a number | ||
| expected: `export default { property: "value" }` | ||
| }, | ||
| { | ||
| property: 'property', | ||
| source: `export default { property: false }`, // property exists and is a boolean | ||
| expected: `export default { property: "value" }` | ||
| }, | ||
| { | ||
| property: 'property', | ||
| source: `export default { property: "test" }`, // property exists and is a string | ||
| expected: `export default { property: "value" }` | ||
| }, | ||
| { | ||
| property: 'property', | ||
| source: `export default { property: [1,2] }`, // property exists and is an array | ||
| expected: `export default { property: "value" }` | ||
| }, | ||
| { | ||
| property: 'property', | ||
| source: `export default { property: null }`, // property exists and is null | ||
| expected: `export default { property: "value" }` | ||
| }, | ||
| { | ||
| property: 'property', | ||
| source: `export default { property: {}}`, // property exists and is an object | ||
| expected: `export default { property: "value" }` | ||
| }, | ||
|
|
||
| // Deep properties (injection 1) | ||
| { | ||
| property: 'property.b.c', | ||
| source: `export default {}`, | ||
| expected: `export default { property: { b: { c: "value" }}}` | ||
| }, | ||
| { | ||
| property: 'property.b.c', | ||
| source: `export default { property: 0 }`, // property exists and is a number | ||
| expected: `export default { property: { b: { c: "value" }}}` | ||
| }, | ||
| { | ||
| property: 'property.b.c', | ||
| source: `export default { property: {}}`, // property exists and is an object | ||
| expected: `export default { property: { b: { c: "value" }}}` | ||
| }, | ||
|
|
||
| // Deep properties (injection 2) | ||
| { | ||
| property: 'property.b.c', | ||
| source: `export default { property: { b: 0 }}`, // property exists and is a number | ||
| expected: `export default { property: { b: { c: "value" }}}` | ||
| }, | ||
| { | ||
| property: 'property.b.c', | ||
| source: `export default { property: { b: {}}}`, // property exists and is an object | ||
| expected: `export default { property: { b: { c: "value" }}}` | ||
| }, | ||
| { | ||
| property: 'property.b.c', | ||
| source: `export default { property: { b: { hello: 123}}}`, // property exists and is a non-empty object | ||
| expected: `export default { property: { b: { c: "value", hello: 123 }}}` | ||
| }, | ||
|
|
||
| // Deep properties (existing properties) | ||
| { | ||
| property: 'a1.a2', | ||
| source: `export default { a2: false, a1: { a3: [12]}}`, // property exists and is a non-empty object | ||
| expected: `export default { a2: false, a1: { a2: "value", a3: [12]}}` | ||
| }, | ||
|
|
||
| // | ||
| // Direct module exports | ||
| // | ||
| { | ||
| property: 'property', | ||
| source: `module.exports = {}`, | ||
| expected: `module.exports = { property: "value"}` | ||
| }, | ||
| { | ||
| property: 'property', | ||
| source: `module.exports = { p1: 0}`, | ||
| expected: `module.exports = { property: "value", p1: 0}` | ||
| }, | ||
| { | ||
| property: 'a.b.c', | ||
| source: `module.exports = { p1: 0}`, | ||
| expected: `module.exports = { a: { b: { c: "value" }}, p1: 0}` | ||
| }, | ||
|
|
||
| // | ||
| // Indirect module exports | ||
| // | ||
| { | ||
| property: 'property', | ||
| source: `const config = {}; module.exports = config`, | ||
| expected: `const config = { property: "value"}; module.exports = config` | ||
| }, | ||
| { | ||
| property: 'property', | ||
| source: `var config = {}; module.exports = config`, | ||
| expected: `var config = { property: "value"}; module.exports = config` | ||
| }, | ||
| { | ||
| property: 'a.b.c', | ||
| source: `var config = {}; module.exports = config`, | ||
| expected: `var config = { a: { b: { c: "value"}}}; module.exports = config` | ||
| }, | ||
| { | ||
| property: 'a.b.c', | ||
| source: `var config = { a: { b: [], c: "hello"}}; module.exports = config`, | ||
| expected: `var config = { a: { b: { c: "value"}, c: "hello"}}; module.exports = config` | ||
| }, | ||
| ] | ||
|
|
||
| describe('config-parser', () => { | ||
| cases.forEach(({property, source, expected}, index) => { | ||
| it(`Inject path properly for case #${index}`, () => { | ||
| // Write the source file | ||
| const sourceFile = `${tempFolder}/source.js` | ||
| fs.writeFileSync(sourceFile, source, {encoding: 'utf8'}) | ||
|
|
||
| // Write the expected file | ||
| const expectedFile = `${tempFolder}/expected.js` | ||
| fs.writeFileSync(expectedFile, expected, {encoding: 'utf8'}) | ||
|
|
||
| // Update the settings and do the injection | ||
| new ConfigParser({ | ||
| configurationFile: sourceFile, | ||
| propertyName: property, | ||
| propertyValue: 'value' | ||
| }).inject() | ||
|
|
||
| // Compare the files | ||
| compareFiles(sourceFile, expectedFile) | ||
| }) | ||
| }) | ||
| }) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| const fs = require('fs') | ||
| const prettier = require('prettier') | ||
| const assert = require('assert') | ||
|
|
||
| // Create and return the path to a temp folder | ||
| function getTempFolder() { | ||
| const tmpFolder = `${__dirname}/fixtures/tmp` | ||
| if (!fs.existsSync(tmpFolder)) { | ||
| fs.mkdirSync(tmpFolder, {recursive: true}) | ||
| } | ||
| return tmpFolder | ||
| } | ||
|
|
||
| // Read a JavaScript file and return it formatted | ||
| function formatFile(file) { | ||
| const fileContent = fs.readFileSync(file, 'utf8') | ||
| return prettier.format(fileContent, { | ||
| parser: 'espree', | ||
|
|
||
| // Prettier options | ||
| printWidth: 80, | ||
| tabWidth: 2, | ||
| useTabs: false, | ||
| semi: false, | ||
| singleQuote: true, | ||
| trailingComma: 'none', | ||
| bracketSpacing: false, | ||
| arrowParens: 'avoid' | ||
| }).trim() | ||
| } | ||
|
|
||
| // Compare two JavaScript files | ||
| function compareFiles(actualFile, expectedFile) { | ||
| assert.equal(formatFile(actualFile), formatFile(expectedFile)) | ||
| } | ||
|
|
||
| module.exports = {getTempFolder, formatFile, compareFiles} |