From f8e320bf67bfd6d13546717e961312fa87dbd257 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder <231804+danez@users.noreply.github.com> Date: Fri, 12 Aug 2022 00:36:32 +0200 Subject: [PATCH 1/2] fix: fix local builds with plugins and build settings from the ui --- src/lib/build.js | 15 ++++ tests/integration/110.command.build.test.js | 79 +++++++++++++++++++-- tests/integration/utils/site-builder.js | 9 +++ 3 files changed, 96 insertions(+), 7 deletions(-) diff --git a/src/lib/build.js b/src/lib/build.js index 096d8485a36..7e241451f12 100644 --- a/src/lib/build.js +++ b/src/lib/build.js @@ -1,4 +1,6 @@ // @ts-check +const process = require('process') + const netlifyBuildPromise = import('@netlify/build') /** @@ -20,6 +22,7 @@ const netlifyBuildPromise = import('@netlify/build') */ const getBuildOptions = ({ cachedConfig, options: { context, cwd, debug, dry, json, offline, silent }, token }) => ({ cachedConfig, + siteId: cachedConfig.siteInfo.id, token, dry, debug, @@ -42,6 +45,18 @@ const getBuildOptions = ({ cachedConfig, options: { context, cwd, debug, dry, js */ const runBuild = async (options) => { const { default: build } = await netlifyBuildPromise + + // If netlify NETLIFY_API_URL is set we need to pass this information to @netlify/build + // TODO don't use testOpts, but add real properties to do this. + if (process.env.NETLIFY_API_URL) { + const apiUrl = new URL(process.env.NETLIFY_API_URL) + const testOpts = { + scheme: apiUrl.protocol.slice(0, -1), + host: apiUrl.host, + } + options = { ...options, testOpts } + } + const { configMutations, netlifyConfig: newConfig, severityCode: exitCode } = await build(options) return { exitCode, newConfig, configMutations } } diff --git a/tests/integration/110.command.build.test.js b/tests/integration/110.command.build.test.js index 0bc9b8941a0..9c2897383f1 100644 --- a/tests/integration/110.command.build.test.js +++ b/tests/integration/110.command.build.test.js @@ -9,7 +9,6 @@ const { withSiteBuilder } = require('./utils/site-builder') const defaultEnvs = { NETLIFY_AUTH_TOKEN: 'fake-token', - NETLIFY_SITE_ID: 'site_id', FORCE_COLOR: '1', } @@ -19,7 +18,7 @@ const defaultEnvs = { const runBuildCommand = async function ( t, cwd, - { apiUrl, exitCode: expectedExitCode = 0, output, flags = [], env = defaultEnvs } = {}, + { apiUrl, exitCode: expectedExitCode = 0, output: outputs, flags = [], env = defaultEnvs } = {}, ) { const { all, exitCode } = await execa(cliPath, ['build', ...flags], { reject: false, @@ -35,7 +34,12 @@ const runBuildCommand = async function ( console.error(all) } - t.true(all.includes(output)) + if (!Array.isArray(outputs)) { + outputs = [outputs] + } + outputs.forEach((output) => { + t.true(all.includes(output), `Output of build command does not include '${output}'`) + }) t.is(exitCode, expectedExitCode) } @@ -44,6 +48,12 @@ const siteInfo = { id: 'site_id', name: 'site-name', } +const siteInfoWithCommand = { + ...siteInfo, + build_settings: { + cmd: 'echo uiCommand', + }, +} const routes = [ { path: 'sites/site_id', response: siteInfo }, { path: 'sites/site_id/service-instances', response: [] }, @@ -52,10 +62,60 @@ const routes = [ response: [{ slug: siteInfo.account_slug }], }, ] +const routesWithCommand = [...routes] +routesWithCommand.splice(0, 1, { path: 'sites/site_id', response: siteInfoWithCommand }) + +test('should use build command from UI', async (t) => { + await withSiteBuilder('success-site', async (builder) => { + builder.withNetlifyToml({ config: {} }).withStateFile({ siteId: siteInfo.id }) + + await builder.buildAsync() + await withMockApi(routesWithCommand, async ({ apiUrl }) => { + await runBuildCommand(t, builder.directory, { apiUrl, output: 'uiCommand' }) + }) + }) +}) + +test('should use build command from UI with build plugin', async (t) => { + await withSiteBuilder('success-site', async (builder) => { + builder + .withNetlifyToml({ + config: { + plugins: [{ package: '/plugins/' }], + }, + }) + .withStateFile({ siteId: siteInfo.id }) + .withBuildPlugin({ + name: 'index', + plugin: { + onPreBuild: ({ netlifyConfig }) => { + console.log('test-pre-build') + + netlifyConfig.build.environment ||= {} + netlifyConfig.build.environment.TEST_123 = '12345' + }, + }, + }) + + await builder.buildAsync() + await withMockApi(routesWithCommand, async ({ apiUrl }) => { + await runBuildCommand(t, builder.directory, { + apiUrl, + output: ['uiCommand', 'test-pre-build'], + env: { + NETLIFY_AUTH_TOKEN: 'fake-token', + FORCE_COLOR: '1', + }, + }) + }) + }) +}) test('should print output for a successful command', async (t) => { await withSiteBuilder('success-site', async (builder) => { - builder.withNetlifyToml({ config: { build: { command: 'echo testCommand' } } }) + builder + .withNetlifyToml({ config: { build: { command: 'echo testCommand' } } }) + .withStateFile({ siteId: siteInfo.id }) await builder.buildAsync() @@ -67,7 +127,7 @@ test('should print output for a successful command', async (t) => { test('should print output for a failed command', async (t) => { await withSiteBuilder('failure-site', async (builder) => { - builder.withNetlifyToml({ config: { build: { command: 'doesNotExist' } } }) + builder.withNetlifyToml({ config: { build: { command: 'doesNotExist' } } }).withStateFile({ siteId: siteInfo.id }) await builder.buildAsync() @@ -79,7 +139,9 @@ test('should print output for a failed command', async (t) => { test('should run in dry mode when the --dry flag is set', async (t) => { await withSiteBuilder('success-site', async (builder) => { - builder.withNetlifyToml({ config: { build: { command: 'echo testCommand' } } }) + builder + .withNetlifyToml({ config: { build: { command: 'echo testCommand' } } }) + .withStateFile({ siteId: siteInfo.id }) await builder.buildAsync() @@ -140,7 +202,9 @@ test('should run the staging context command when the context env variable is se test('should print debug information when the --debug flag is set', async (t) => { await withSiteBuilder('success-site', async (builder) => { - builder.withNetlifyToml({ config: { build: { command: 'echo testCommand' } } }) + builder + .withNetlifyToml({ config: { build: { command: 'echo testCommand' } } }) + .withStateFile({ siteId: siteInfo.id }) await builder.buildAsync() @@ -154,6 +218,7 @@ test('should use root directory netlify.toml when runs in subdirectory', async ( await withSiteBuilder('subdir-site', async (builder) => { builder .withNetlifyToml({ config: { build: { command: 'echo testCommand' } } }) + .withStateFile({ siteId: siteInfo.id }) .withContentFile({ path: path.join('subdir', '.gitkeep'), content: '' }) await builder.buildAsync() diff --git a/tests/integration/utils/site-builder.js b/tests/integration/utils/site-builder.js index 4e3cfd88579..bb55feb351d 100644 --- a/tests/integration/utils/site-builder.js +++ b/tests/integration/utils/site-builder.js @@ -48,6 +48,15 @@ const createSiteBuilder = ({ siteName }) => { }) return builder }, + withStateFile: ({ siteId = '' }) => { + const dest = path.join(directory, '.netlify', 'state.json') + tasks.push(async () => { + const content = `{ "siteId" : "${siteId}" }` + await ensureDir(path.dirname(dest)) + await writeFile(dest, content) + }) + return builder + }, withPackageJson: ({ packageJson, pathPrefix = '' }) => { const dest = path.join(directory, pathPrefix, 'package.json') tasks.push(async () => { From 8641e6e41de3d5be18afc8896a2ea3bd565c1442 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder <231804+danez@users.noreply.github.com> Date: Fri, 12 Aug 2022 11:50:37 +0200 Subject: [PATCH 2/2] chore: fix test for node 12 --- tests/integration/110.command.build.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/110.command.build.test.js b/tests/integration/110.command.build.test.js index 9c2897383f1..a1d38c04f92 100644 --- a/tests/integration/110.command.build.test.js +++ b/tests/integration/110.command.build.test.js @@ -91,7 +91,7 @@ test('should use build command from UI with build plugin', async (t) => { onPreBuild: ({ netlifyConfig }) => { console.log('test-pre-build') - netlifyConfig.build.environment ||= {} + netlifyConfig.build.environment = netlifyConfig.build.environment || {} netlifyConfig.build.environment.TEST_123 = '12345' }, },