mirror of
https://github.com/actions/configure-pages.git
synced 2025-12-08 16:16:09 +00:00
Add tests for config-parser
This commit is contained in:
6
dist/index.js
vendored
6
dist/index.js
vendored
@@ -14632,9 +14632,9 @@ class ConfigParser {
|
|||||||
// The last node identified is an object expression, so do the assignment
|
// The last node identified is an object expression, so do the assignment
|
||||||
if (lastNode.type === 'ObjectExpression') {
|
if (lastNode.type === 'ObjectExpression') {
|
||||||
this.configuration =
|
this.configuration =
|
||||||
this.configuration.slice(0, lastNode.value.range[0]) +
|
this.configuration.slice(0, lastNode.range[0]) +
|
||||||
`"${this.propertyValue}"` +
|
`"${this.propertyValue}"` +
|
||||||
this.configuration.slice(lastNode.value.range[1])
|
this.configuration.slice(lastNode.range[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
// A misc object was found in the configuration file (e.g. an array, a string, a boolean,
|
// A misc object was found in the configuration file (e.g. an array, a string, a boolean,
|
||||||
@@ -14679,7 +14679,7 @@ class ConfigParser {
|
|||||||
else {
|
else {
|
||||||
this.configuration =
|
this.configuration =
|
||||||
this.configuration.slice(0, lastNode.range[0]) +
|
this.configuration.slice(0, lastNode.range[0]) +
|
||||||
declaration +
|
'{' + declaration + '}' +
|
||||||
this.configuration.slice(lastNode.range[1])
|
this.configuration.slice(lastNode.range[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
@@ -202,9 +202,9 @@ class ConfigParser {
|
|||||||
// The last node identified is an object expression, so do the assignment
|
// The last node identified is an object expression, so do the assignment
|
||||||
if (lastNode.type === 'ObjectExpression') {
|
if (lastNode.type === 'ObjectExpression') {
|
||||||
this.configuration =
|
this.configuration =
|
||||||
this.configuration.slice(0, lastNode.value.range[0]) +
|
this.configuration.slice(0, lastNode.range[0]) +
|
||||||
`"${this.propertyValue}"` +
|
`"${this.propertyValue}"` +
|
||||||
this.configuration.slice(lastNode.value.range[1])
|
this.configuration.slice(lastNode.range[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
// A misc object was found in the configuration file (e.g. an array, a string, a boolean,
|
// A misc object was found in the configuration file (e.g. an array, a string, a boolean,
|
||||||
@@ -249,7 +249,7 @@ class ConfigParser {
|
|||||||
else {
|
else {
|
||||||
this.configuration =
|
this.configuration =
|
||||||
this.configuration.slice(0, lastNode.range[0]) +
|
this.configuration.slice(0, lastNode.range[0]) +
|
||||||
declaration +
|
'{' + declaration + '}' +
|
||||||
this.configuration.slice(lastNode.range[1])
|
this.configuration.slice(lastNode.range[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
158
src/config-parser.test.js
Normal file
158
src/config-parser.test.js
Normal file
@@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,101 +0,0 @@
|
|||||||
// const {ConfigParser} = require('./config-parser')
|
|
||||||
// const fs = require('fs')
|
|
||||||
// const assert = require('assert')
|
|
||||||
|
|
||||||
// const srcFolder = `${process.cwd()}/src/fixtures`
|
|
||||||
// const tmpFolder = `${process.cwd()}/src/fixtures/tmp`
|
|
||||||
// const expectedFolder = `${process.cwd()}/src/fixtures/expected`
|
|
||||||
|
|
||||||
// const repoPath = '/amazing-new-repo/'
|
|
||||||
|
|
||||||
// const cases = [
|
|
||||||
// [
|
|
||||||
// 'next.config.js',
|
|
||||||
// {
|
|
||||||
// configurationFile: `${tmpFolder}/next.config.js`,
|
|
||||||
// propertyName: 'basePath',
|
|
||||||
// propertyValue: repoPath,
|
|
||||||
// blankConfigurationFile: `${process.cwd()}/src/blank-configurations/next.js`
|
|
||||||
// }
|
|
||||||
// ],
|
|
||||||
// [
|
|
||||||
// 'next.config.old.js',
|
|
||||||
// {
|
|
||||||
// configurationFile: `${tmpFolder}/next.config.old.js`,
|
|
||||||
// propertyName: 'basePath',
|
|
||||||
// propertyValue: repoPath,
|
|
||||||
// blankConfigurationFile: `${process.cwd()}/src/blank-configurations/next.js`
|
|
||||||
// }
|
|
||||||
// ],
|
|
||||||
// [
|
|
||||||
// 'next.config.old.missing.js',
|
|
||||||
// {
|
|
||||||
// configurationFile: `${tmpFolder}/next.config.old.missing.js`,
|
|
||||||
// propertyName: 'basePath',
|
|
||||||
// propertyValue: repoPath,
|
|
||||||
// blankConfigurationFile: `${process.cwd()}/src/blank-configurations/next.js`
|
|
||||||
// }
|
|
||||||
// ],
|
|
||||||
// [
|
|
||||||
// 'gatsby-config.js',
|
|
||||||
// {
|
|
||||||
// configurationFile: `${tmpFolder}/gatsby-config.js`,
|
|
||||||
// propertyName: 'pathPrefix',
|
|
||||||
// propertyValue: repoPath,
|
|
||||||
// blankConfigurationFile: `${process.cwd()}/src/blank-configurations/gatsby.js`
|
|
||||||
// }
|
|
||||||
// ],
|
|
||||||
// [
|
|
||||||
// 'gatsby-config.old.js',
|
|
||||||
// {
|
|
||||||
// configurationFile: `${tmpFolder}/gatsby-config.old.js`,
|
|
||||||
// propertyName: 'pathPrefix',
|
|
||||||
// propertyValue: repoPath,
|
|
||||||
// blankConfigurationFile: `${process.cwd()}/src/blank-configurations/gatsby.js`
|
|
||||||
// }
|
|
||||||
// ],
|
|
||||||
// [
|
|
||||||
// 'nuxt.config.js',
|
|
||||||
// {
|
|
||||||
// configurationFile: `${tmpFolder}/nuxt.config.js`,
|
|
||||||
// propertyName: 'router.base',
|
|
||||||
// propertyValue: repoPath,
|
|
||||||
// blankConfigurationFile: `${process.cwd()}/src/blank-configurations/nuxt.js`
|
|
||||||
// }
|
|
||||||
// ],
|
|
||||||
// [
|
|
||||||
// 'nuxt.config.missing.js',
|
|
||||||
// {
|
|
||||||
// configurationFile: `${tmpFolder}/nuxt.config.missing.js`,
|
|
||||||
// propertyName: 'router.base',
|
|
||||||
// propertyValue: repoPath,
|
|
||||||
// blankConfigurationFile: `${process.cwd()}/src/blank-configurations/nuxt.js`
|
|
||||||
// }
|
|
||||||
// ],
|
|
||||||
// [
|
|
||||||
// 'nuxt.config.old.js',
|
|
||||||
// {
|
|
||||||
// configurationFile: `${tmpFolder}/nuxt.config.old.js`,
|
|
||||||
// propertyName: 'router.base',
|
|
||||||
// propertyValue: repoPath,
|
|
||||||
// blankConfigurationFile: `${process.cwd()}/src/blank-configurations/nuxt.js`
|
|
||||||
// }
|
|
||||||
// ]
|
|
||||||
// ]
|
|
||||||
|
|
||||||
// describe('configParser', () => {
|
|
||||||
// test.each(cases)('%p parsed correctly', (fileName, configuration) => {
|
|
||||||
// srcFileName = `${srcFolder}/${fileName}`
|
|
||||||
// tmpFileName = `${tmpFolder}/${fileName}`
|
|
||||||
// expectedFileName = `${expectedFolder}/${fileName}`
|
|
||||||
// fs.mkdirSync(tmpFolder, {recursive: true})
|
|
||||||
// fs.copyFileSync(srcFileName, tmpFileName)
|
|
||||||
// const parser = new ConfigParser(configuration)
|
|
||||||
// parser.parse()
|
|
||||||
|
|
||||||
// var expectedContent = fs.readFileSync(expectedFileName).toString()
|
|
||||||
// var actualContent = fs.readFileSync(tmpFileName).toString()
|
|
||||||
// assert.equal(actualContent, expectedContent)
|
|
||||||
// fs.rmSync(tmpFileName)
|
|
||||||
// })
|
|
||||||
// })
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
const core = require('@actions/core')
|
const core = require('@actions/core')
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
//const { expect, jest } = require('@jest/globals')
|
|
||||||
|
|
||||||
const getPagesBaseUrl = require('./get-pages-base-url')
|
const getPagesBaseUrl = require('./get-pages-base-url')
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +1,12 @@
|
|||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const assert = require('assert')
|
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const prettier = require('prettier')
|
|
||||||
|
|
||||||
const {getConfigParserSettings} = require('./set-pages-path')
|
const {getConfigParserSettings} = require('./set-pages-path')
|
||||||
const {ConfigParser} = require('./config-parser')
|
const {ConfigParser} = require('./config-parser')
|
||||||
|
const {getTempFolder, compareFiles} = require('./test-helpers')
|
||||||
|
|
||||||
// Temp folder for fixtures to be injected
|
// Get the temp folder
|
||||||
const tmpFolder = `${process.cwd()}/src/fixtures/tmp`
|
const tempFolder = getTempFolder()
|
||||||
if (!fs.existsSync(tmpFolder)) {
|
|
||||||
fs.mkdirSync(tmpFolder, {recursive: true})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read a JavaScript file and return it formatted
|
|
||||||
function formatFile(file) {
|
|
||||||
const fileContent = fs.readFileSync(file, 'utf8')
|
|
||||||
return prettier.format(fileContent, {parser: 'espree'})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare two JavaScript files
|
|
||||||
function compareFiles(actualFile, expectedFile) {
|
|
||||||
assert.equal(formatFile(actualFile), formatFile(expectedFile))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test suite
|
// Test suite
|
||||||
describe('configParser', () => {
|
describe('configParser', () => {
|
||||||
@@ -43,7 +28,7 @@ describe('configParser', () => {
|
|||||||
|
|
||||||
// Copy the source fixture to a temp file
|
// Copy the source fixture to a temp file
|
||||||
const fixtureSourceFile = `${fixtureFolder}/${configurationFile}`
|
const fixtureSourceFile = `${fixtureFolder}/${configurationFile}`
|
||||||
const fixtureTargetFile = `${tmpFolder}/${configurationFile}`
|
const fixtureTargetFile = `${tempFolder}/${configurationFile}`
|
||||||
if (configurationFile != 'blank.js') {
|
if (configurationFile != 'blank.js') {
|
||||||
fs.copyFileSync(fixtureSourceFile, fixtureTargetFile)
|
fs.copyFileSync(fixtureSourceFile, fixtureTargetFile)
|
||||||
} else if (fs.existsSync(fixtureTargetFile)) {
|
} else if (fs.existsSync(fixtureTargetFile)) {
|
||||||
|
|||||||
37
src/test-helpers.js
Normal file
37
src/test-helpers.js
Normal file
@@ -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}
|
||||||
Reference in New Issue
Block a user