Add set-pages-path tests

This commit is contained in:
Yoann Chaudet
2022-07-19 11:47:29 -07:00
parent 43a5456c3f
commit ba4576e776
26 changed files with 317 additions and 168 deletions

View File

@@ -22,11 +22,11 @@
"axios": "^0.27.2", "axios": "^0.27.2",
"axios-retry": "^3.2.5", "axios-retry": "^3.2.5",
"espree": "^9.3.2", "espree": "^9.3.2",
"prettier": "^2.7.1",
"string-format": "^1.0.0" "string-format": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {
"@vercel/ncc": "^0.34.0", "@vercel/ncc": "^0.34.0",
"jest": "^28.1.1" "jest": "^28.1.1",
"prettier": "^2.7.1"
} }
} }

View File

@@ -158,7 +158,8 @@ class ConfigParser {
} }
} }
parse() { // Parse a configuration file and try to inject Pages settings in it.
inject() {
// Logging // Logging
core.info(`Parsing configuration:\n${this.configuration}`) core.info(`Parsing configuration:\n${this.configuration}`)

View File

@@ -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)
})
})

101
src/config-parser.testx.js Normal file
View File

@@ -0,0 +1,101 @@
// 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)
// })
// })

View File

@@ -9,7 +9,8 @@ function getRequiredVars() {
} }
} }
module.exports = function getContext() { // Return the context object
function getContext() {
const requiredVars = getRequiredVars() const requiredVars = getRequiredVars()
for (const variable in requiredVars) { for (const variable in requiredVars) {
if (requiredVars[variable] === undefined) { if (requiredVars[variable] === undefined) {
@@ -19,3 +20,5 @@ module.exports = function getContext() {
core.debug('all variables are set') core.debug('all variables are set')
return requiredVars return requiredVars
} }
module.exports = {getContext}

View File

@@ -1,8 +0,0 @@
import {resolve} from 'path'
export default {
alias: {
style: resolve(__dirname, './assets/style')
},
pathPrefix: '/amazing-new-repo/' /* test */
}

View File

@@ -1,3 +0,0 @@
module.exports = {
pathPrefix: '/amazing-new-repo/'
}

View File

@@ -1,8 +0,0 @@
import {resolve} from 'path'
export default {
alias: {
style: resolve(__dirname, './assets/style')
},
basePath: '/amazing-new-repo/' /* test */
}

View File

@@ -1,3 +0,0 @@
module.exports = {
basePath: '/amazing-new-repo/' /* test */
}

View File

@@ -1 +0,0 @@
module.exports = {basePath: '/amazing-new-repo/'}

View File

@@ -1,11 +0,0 @@
import {resolve} from 'path'
export default {
alias: {
style: resolve(__dirname, './assets/style')
},
target: 'static',
router: {
base: '/amazing-new-repo/'
}
}

View File

@@ -1,9 +0,0 @@
import {resolve} from 'path'
export default {
router: {base: '/amazing-new-repo/'},
alias: {
style: resolve(__dirname, './assets/style')
},
target: 'static'
}

View File

@@ -1,9 +0,0 @@
module.exports = {
alias: {
style: resolve(__dirname, './assets/style')
},
target: 'static',
router: {
base: '/amazing-new-repo/'
}
}

View File

@@ -0,0 +1,2 @@
// Default Pages configuration for Gatsby
module.exports = { pathPrefix: "/docs/" }

View File

@@ -0,0 +1 @@
// This file is not read by the test suite

View File

@@ -0,0 +1,8 @@
module.exports = {
pathPrefix: "/docs/",
siteMetadata: {
title: `My Gatsby Site`,
siteUrl: `https://www.yourdomain.tld`,
},
plugins: [],
}

View File

@@ -0,0 +1,7 @@
module.exports = {
siteMetadata: {
title: `My Gatsby Site`,
siteUrl: `https://www.yourdomain.tld`,
},
plugins: [],
}

View File

@@ -0,0 +1,3 @@
// Default Pages configuration for Next
const nextConfig = { basePath: "/docs/" }
module.exports = nextConfig

View File

@@ -0,0 +1 @@
// This file is not read by the test suite

View File

@@ -0,0 +1,8 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
basePath: "/docs/",
reactStrictMode: true,
swcMinify: true,
}
module.exports = nextConfig

View File

@@ -0,0 +1,7 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
}
module.exports = nextConfig

View File

@@ -0,0 +1,45 @@
export default {
// Disable server-side rendering: https://go.nuxtjs.dev/ssr-mode
router: { base: "/docs/" },
ssr: false,
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
title: 'nuxt',
htmlAttrs: {
lang: 'en'
},
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: '' },
{ name: 'format-detection', content: 'telephone=no' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
]
},
// Global CSS: https://go.nuxtjs.dev/config-css
css: [
],
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: [
],
// Auto import components: https://go.nuxtjs.dev/config-components
components: true,
// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
buildModules: [
],
// Modules: https://go.nuxtjs.dev/config-modules
modules: [
],
// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
}
}

View File

@@ -0,0 +1,44 @@
export default {
// Disable server-side rendering: https://go.nuxtjs.dev/ssr-mode
ssr: false,
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
title: 'nuxt',
htmlAttrs: {
lang: 'en'
},
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: '' },
{ name: 'format-detection', content: 'telephone=no' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
]
},
// Global CSS: https://go.nuxtjs.dev/config-css
css: [
],
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: [
],
// Auto import components: https://go.nuxtjs.dev/config-components
components: true,
// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
buildModules: [
],
// Modules: https://go.nuxtjs.dev/config-modules
modules: [
],
// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
}
}

View File

@@ -1,3 +0,0 @@
module.exports = {
pathPrefix: "hello",
};

View File

@@ -1,39 +1,45 @@
const core = require('@actions/core') const core = require('@actions/core')
const {ConfigParser} = require('./config-parser') const {ConfigParser} = require('./config-parser')
function getParserConfiguration(staticSiteGenerator, path) { // Return the settings to be passed to a {ConfigParser} for a given
// static site generator and a Pages path value to inject
function getConfigParserSettings(staticSiteGenerator, path) {
switch (staticSiteGenerator) { switch (staticSiteGenerator) {
case 'nuxt': case 'nuxt':
return { return {
configurationFile: './nuxt.config.js', configurationFile: './nuxt.config.js',
propertyName: 'router.base', propertyName: 'router.base',
propertyValue: path, propertyValue: path,
blankConfigurationFile: `${process.cwd()}/blank-configurations/nuxt.js` blankConfigurationFile: `${__dirname}/blank-configurations/nuxt.js`
} }
case 'next': case 'next':
return { return {
configurationFile: './next.config.js', configurationFile: './next.config.js',
propertyName: 'basePath', propertyName: 'basePath',
propertyValue: path, propertyValue: path,
blankConfigurationFile: `${process.cwd()}/blank-configurations/next.js` blankConfigurationFile: `${__dirname}/blank-configurations/next.js`
} }
case 'gatsby': case 'gatsby':
return { return {
configurationFile: './gatsby-config.js', configurationFile: './gatsby-config.js',
propertyName: 'pathPrefix', propertyName: 'pathPrefix',
propertyValue: path, propertyValue: path,
blankConfigurationFile: `${process.cwd()}/blank-configurations/gatsby.js` blankConfigurationFile: `${__dirname}/blank-configurations/gatsby.js`
} }
default: default:
throw `Unsupported static site generator: ${staticSiteGenerator}` throw `Unsupported static site generator: ${staticSiteGenerator}`
} }
} }
async function setPagesPath({staticSiteGenerator, path}) { // Inject Pages configuration in a given static site generator's configuration file
function setPagesPath({staticSiteGenerator, path}) {
try { try {
// Parse/mutate the configuration file // Parse the configuration file and try to inject the Pages configuration in it
new ConfigParser(getParserConfiguration(staticSiteGenerator, path)).parse() new ConfigParser(
getConfigParserSettings(staticSiteGenerator, path)
).inject()
} catch (error) { } catch (error) {
// Logging
core.warning( core.warning(
`We were unable to determine how to inject the site metadata into your config. Generated URLs may be incorrect. The base URL for this site should be ${path}. Please ensure your framework is configured to generate relative links appropriately.`, `We were unable to determine how to inject the site metadata into your config. Generated URLs may be incorrect. The base URL for this site should be ${path}. Please ensure your framework is configured to generate relative links appropriately.`,
error error
@@ -41,4 +47,4 @@ async function setPagesPath({staticSiteGenerator, path}) {
} }
} }
module.exports = setPagesPath module.exports = {getConfigParserSettings, setPagesPath}

View File

@@ -0,0 +1,68 @@
const fs = require('fs')
const assert = require('assert')
const path = require('path')
const prettier = require('prettier')
const {getConfigParserSettings} = require('./set-pages-path')
const {ConfigParser} = require('./config-parser')
// Temp folder for fixtures to be injected
const tmpFolder = `${process.cwd()}/src/fixtures/tmp`
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
describe('configParser', () => {
// Iterate over the static site generators
;['next', 'nuxt', 'gatsby'].forEach(staticSiteGenerator => {
// Folder containing the fixtures for a given static site generator
const fixtureFolder = `${__dirname}/fixtures/${staticSiteGenerator}`
// Iterate over the fixtures
fs.readdirSync(fixtureFolder).forEach(configurationFile => {
// Ignore expectation
if (configurationFile.endsWith('.expected.js')) {
return
}
it(`Inject path properly for ${staticSiteGenerator} in ${configurationFile}`, async () => {
// Get settings for the static site generator
const settings = getConfigParserSettings(staticSiteGenerator, '/docs/')
// Copy the source fixture to a temp file
const fixtureSourceFile = `${fixtureFolder}/${configurationFile}`
const fixtureTargetFile = `${tmpFolder}/${configurationFile}`
if (configurationFile != 'blank.js') {
fs.copyFileSync(fixtureSourceFile, fixtureTargetFile)
} else {
fs.rmSync(fixtureTargetFile)
}
// Update the settings and do the injection
settings.configurationFile = fixtureTargetFile
new ConfigParser(settings).inject()
// Read the expected file
const expectedFile = `${fixtureFolder}/${path.basename(
configurationFile,
'.js'
)}.expected.js`
// Compare the actual and expected files
compareFiles(settings.configurationFile, expectedFile)
})
})
})
})