diff --git a/tests/build.test.js b/tests/build.test.js deleted file mode 100644 index 17fb26d81c8..00000000000 --- a/tests/build.test.js +++ /dev/null @@ -1,70 +0,0 @@ -const path = require('path') -const test = require('ava') -const execa = require('execa') - -const BIN_PATH = path.join(__dirname, '..', 'bin', 'run') -const FIXTURE_DIR = __dirname - -// Runs `netlify build ...flags` then verify: -// - its exit code is `exitCode` -// - that its output contains `output` -// The command is run in the fixture directory `fixtureSubDir`. -const runBuildCommand = async function( - t, - fixtureSubDir, - { exitCode: expectedExitCode = 0, output, flags = [], env } = {} -) { - const { all, exitCode } = await execa(BIN_PATH, ['build', ...flags], { - reject: false, - cwd: `${FIXTURE_DIR}/${fixtureSubDir}`, - env: { FORCE_COLOR: '1', NETLIFY_AUTH_TOKEN: 'test', ...env }, - all: true, - }) - - if (exitCode !== expectedExitCode) { - console.error(all) - } - - t.true(all.includes(output)) - t.is(exitCode, expectedExitCode) -} - -test('build command - succeeds', async t => { - await runBuildCommand(t, 'success-site', { output: 'testCommand' }) -}) - -test('build command - fails', async t => { - await runBuildCommand(t, 'failure-site', { exitCode: 1, output: 'doesNotExist' }) -}) - -test('build command - uses the CLI mode', async t => { - await runBuildCommand(t, 'success-site', { output: 'mode: cli' }) -}) - -test('build command - can use the --dry flag', async t => { - await runBuildCommand(t, 'success-site', { flags: ['--dry'], output: 'If this looks good to you' }) -}) - -test('build command - can use the --context flag', async t => { - await runBuildCommand(t, 'context-site', { flags: ['--context=staging'], output: 'testStaging' }) -}) - -test('build command - can use the --debug flag', async t => { - await runBuildCommand(t, 'success-site', { flags: ['--debug'], output: 'Resolved config' }) -}) - -test('build command - can run in subdirectories', async t => { - await runBuildCommand(t, 'subdir-site/subdir', { output: 'testCommand' }) -}) - -test('build command - wrong config', async t => { - await runBuildCommand(t, 'wrong-config-site', { exitCode: 1, output: 'Invalid syntax' }) -}) - -test('build command - missing siteId', async t => { - await runBuildCommand(t, 'success-site', { - exitCode: 1, - output: 'Could not find the site ID', - env: { NODE_ENV: '' }, - }) -}) diff --git a/tests/command.build.test.js b/tests/command.build.test.js new file mode 100644 index 00000000000..81b8524993c --- /dev/null +++ b/tests/command.build.test.js @@ -0,0 +1,127 @@ +const path = require('path') +const test = require('ava') +const execa = require('execa') +const { withSiteBuilder } = require('./utils/siteBuilder') + +const cliPath = require('./utils/cliPath') + +// Runs `netlify build ...flags` then verify: +// - its exit code is `exitCode` +// - that its output contains `output` +// The command is run in the fixture directory `fixtureSubDir`. +const runBuildCommand = async function(t, cwd, { exitCode: expectedExitCode = 0, output, flags = [], env } = {}) { + const { all, exitCode } = await execa(cliPath, ['build', ...flags], { + reject: false, + cwd, + env: { FORCE_COLOR: '1', NETLIFY_AUTH_TOKEN: 'test', ...env }, + all: true, + }) + + if (exitCode !== expectedExitCode) { + console.error(all) + } + + t.true(all.includes(output)) + t.is(exitCode, expectedExitCode) +} + +test('should print output for a successful command', async t => { + await withSiteBuilder('success-site', async builder => { + builder.withNetlifyToml({ config: { build: { command: 'echo testCommand' } } }) + + await builder.buildAsync() + + await runBuildCommand(t, builder.directory, { output: 'testCommand' }) + }) +}) + +test('should print output for a failed command', async t => { + await withSiteBuilder('failure-site', async builder => { + builder.withNetlifyToml({ config: { build: { command: 'doesNotExist' } } }) + + await builder.buildAsync() + + await runBuildCommand(t, builder.directory, { exitCode: 1, output: 'doesNotExist' }) + }) +}) + +test('should set the build mode to cli', async t => { + await withSiteBuilder('success-site', async builder => { + builder.withNetlifyToml({ config: { build: { command: 'echo testCommand' } } }) + + await builder.buildAsync() + + await runBuildCommand(t, builder.directory, { output: 'mode: cli' }) + }) +}) + +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' } } }) + + await builder.buildAsync() + + await runBuildCommand(t, builder.directory, { flags: ['--dry'], output: 'If this looks good to you' }) + }) +}) + +test('should run the staging context command when the --context option is set to staging', async t => { + await withSiteBuilder('context-site', async builder => { + builder.withNetlifyToml({ + config: { + build: { command: 'echo testCommand' }, + context: { staging: { command: 'echo testStaging' } }, + }, + }) + + await builder.buildAsync() + + await runBuildCommand(t, builder.directory, { flags: ['--context=staging'], output: 'testStaging' }) + }) +}) + +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' } } }) + + await builder.buildAsync() + + await runBuildCommand(t, builder.directory, { flags: ['--debug'], output: 'Resolved config' }) + }) +}) + +test('should use root directory netlify.toml when runs in subdirectory', async t => { + await withSiteBuilder('subdir-site', async builder => { + builder + .withNetlifyToml({ config: { build: { command: 'echo testCommand' } } }) + .withContentFile({ path: path.join('subdir', '.gitkeep'), content: '' }) + + await builder.buildAsync() + + await runBuildCommand(t, path.join(builder.directory, 'subdir'), { output: 'testCommand' }) + }) +}) + +test('should error when using invalid netlify.toml', async t => { + await withSiteBuilder('wrong-config-site', async builder => { + builder.withNetlifyToml({ config: { build: { command: false } } }) + + await builder.buildAsync() + + await runBuildCommand(t, builder.directory, { exitCode: 1, output: 'Invalid syntax' }) + }) +}) + +test('should error when a site id is missing', async t => { + await withSiteBuilder('success-site', async builder => { + builder.withNetlifyToml({ config: { build: { command: 'echo testCommand' } } }) + + await builder.buildAsync() + + await runBuildCommand(t, builder.directory, { + exitCode: 1, + output: 'Could not find the site ID', + env: { NODE_ENV: '' }, + }) + }) +}) diff --git a/tests/context-site/netlify.toml b/tests/context-site/netlify.toml deleted file mode 100644 index 0bcd71ebdc4..00000000000 --- a/tests/context-site/netlify.toml +++ /dev/null @@ -1,5 +0,0 @@ -[build] -command = "echo testCommand" - -[context.staging] -command = "echo testStaging" diff --git a/tests/failure-site/netlify.toml b/tests/failure-site/netlify.toml deleted file mode 100644 index 9c158e14646..00000000000 --- a/tests/failure-site/netlify.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build] -command = "doesNotExist" diff --git a/tests/subdir-site/netlify.toml b/tests/subdir-site/netlify.toml deleted file mode 100644 index 440b1ba6692..00000000000 --- a/tests/subdir-site/netlify.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build] -command = "echo testCommand" diff --git a/tests/subdir-site/subdir/.gitkeep b/tests/subdir-site/subdir/.gitkeep deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/success-site/netlify.toml b/tests/success-site/netlify.toml deleted file mode 100644 index 440b1ba6692..00000000000 --- a/tests/success-site/netlify.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build] -command = "echo testCommand" diff --git a/tests/wrong-config-site/netlify.toml b/tests/wrong-config-site/netlify.toml deleted file mode 100644 index ce7e822d86f..00000000000 --- a/tests/wrong-config-site/netlify.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build] -command = false