[now-cli] Add e2e test for vercel.json and .vercelignore (#4292)

This PR adds a test for a deployment as well as `now dev` to ensure both `vercel.json` and `.vercelignore` are applied.

I also fixed the remaining test helpers to work with `vercel.json`.
This commit is contained in:
Steven
2020-05-07 14:04:37 -04:00
committed by GitHub
parent ad19021969
commit f459db9f83
10 changed files with 41 additions and 28 deletions

View File

@@ -0,0 +1,2 @@
.vercel
public

View File

@@ -0,0 +1,3 @@
.vercel
public
api/two.js

View File

@@ -0,0 +1 @@
module.exports = (_req, res) => res.end('One');

View File

@@ -0,0 +1 @@
module.exports = (_req, res) => res.end('Two');

View File

@@ -0,0 +1,5 @@
<h1>Home Page</h1>
<a href="/api/one">1. Should exist</a>
<a href="/api/two">2. Should NOT exist</a>
<a href="/api/three">3. Should rewrite to one</a>

View File

@@ -0,0 +1,3 @@
{
"rewrites": [{ "source": "/api/three", "destination": "/api/one" }]
}

View File

@@ -232,34 +232,22 @@ function testFixtureStdio(
{ expectedCode = 0, skipDeploy } = {} { expectedCode = 0, skipDeploy } = {}
) { ) {
return async t => { return async t => {
const dir = fixture(directory); const cwd = fixtureAbsolute(directory);
const token = await fetchTokenWithRetry(); const token = await fetchTokenWithRetry();
let deploymentUrl; let deploymentUrl;
// Deploy fixture and link project // Deploy fixture and link project
if (!skipDeploy) { if (!skipDeploy) {
const project = join( const project = join(cwd, '.vercel', 'project.json');
fixtureAbsolute(directory),
'.vercel',
'project.json'
);
if (await fs.exists(project)) { if (await fs.exists(project)) {
await fs.unlink(project); await fs.unlink(project);
} }
const gitignore = join(fixtureAbsolute(directory), '.gitignore'); const gitignore = join(cwd, '.gitignore');
const gitignoreOrig = await fs.exists(gitignore); const gitignoreOrig = await fs.exists(gitignore);
let { stdout, stderr, exitCode } = await execa( let { stdout, stderr, exitCode } = await execa(
binaryPath, binaryPath,
[ ['-t', token, '--confirm', '--public', '--no-clipboard', '--debug'],
dir, { cwd, reject: false }
'-t',
token,
'--confirm',
'--public',
'--no-clipboard',
'--debug',
],
{ reject: false }
); );
console.log({ stdout, stderr, exitCode }); console.log({ stdout, stderr, exitCode });
if (!gitignoreOrig && (await fs.exists(gitignore))) { if (!gitignoreOrig && (await fs.exists(gitignore))) {
@@ -274,7 +262,7 @@ function testFixtureStdio(
// Start dev // Start dev
let dev; let dev;
await runNpmInstall(dir); await runNpmInstall(cwd);
const stdoutList = []; const stdoutList = [];
const stderrList = []; const stderrList = [];
@@ -289,11 +277,10 @@ function testFixtureStdio(
const env = skipDeploy const env = skipDeploy
? { ...process.env, __VERCEL_SKIP_DEV_CMD: 1 } ? { ...process.env, __VERCEL_SKIP_DEV_CMD: 1 }
: process.env; : process.env;
dev = execa( dev = execa(binaryPath, ['dev', '-l', port, '-t', token, '--debug'], {
binaryPath, cwd,
['dev', dir, '-l', port, '-t', token, '--debug'], env,
{ env } });
);
dev.stdout.on('data', data => { dev.stdout.on('data', data => {
stdoutList.push(data); stdoutList.push(data);
@@ -1285,6 +1272,15 @@ test(
) )
); );
test(
'[now dev] 28-vercel-json-and-ignore',
testFixtureStdio('28-vercel-json-and-ignore', async testPath => {
await testPath(200, '/api/one', 'One');
await testPath(404, '/api/two');
await testPath(200, '/api/three', 'One');
})
);
test( test(
'[now dev] Use `@now/python` with Flask requirements.txt', '[now dev] Use `@now/python` with Flask requirements.txt',
testFixtureStdio('python-flask', async testPath => { testFixtureStdio('python-flask', async testPath => {

View File

@@ -7,7 +7,7 @@ const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function nowDeploy(bodies, randomness) { async function nowDeploy(bodies, randomness) {
const files = Object.keys(bodies) const files = Object.keys(bodies)
.filter(n => n !== 'now.json') .filter(n => n !== 'vercel.json' && n !== 'now.json')
.map(n => ({ .map(n => ({
sha: digestOfFile(bodies[n]), sha: digestOfFile(bodies[n]),
size: bodies[n].length, size: bodies[n].length,
@@ -16,7 +16,7 @@ async function nowDeploy(bodies, randomness) {
})); }));
const { FORCE_BUILD_IN_REGION, NOW_DEBUG, VERCEL_DEBUG } = process.env; const { FORCE_BUILD_IN_REGION, NOW_DEBUG, VERCEL_DEBUG } = process.env;
const nowJson = JSON.parse(bodies['now.json']); const nowJson = JSON.parse(bodies['vercel.json'] || bodies['now.json']);
const nowDeployPayload = { const nowDeployPayload = {
version: 2, version: 2,

View File

@@ -53,8 +53,10 @@ async function testDeployment(
); );
} }
const configName = 'vercel.json' in bodies ? 'vercel.json' : 'now.json';
// we use json5 to allow comments for probes // we use json5 to allow comments for probes
const nowJson = json5.parse(bodies['vercel.json'] || bodies['now.json']); const nowJson = json5.parse(bodies[configName]);
if (process.env.VERCEL_BUILDER_DEBUG) { if (process.env.VERCEL_BUILDER_DEBUG) {
if (!nowJson.build) { if (!nowJson.build) {
@@ -90,7 +92,7 @@ async function testDeployment(
} }
} }
bodies['now.json'] = Buffer.from(JSON.stringify(nowJson)); bodies[configName] = Buffer.from(JSON.stringify(nowJson));
delete bodies['probe.js']; delete bodies['probe.js'];
const { deploymentId, deploymentUrl } = await nowDeploy(bodies, randomness); const { deploymentId, deploymentUrl } = await nowDeploy(bodies, randomness);

View File

@@ -10,7 +10,7 @@ function runAnalyze(wrapper, context) {
async function runBuildLambda(inputPath) { async function runBuildLambda(inputPath) {
const inputFiles = await glob('**', inputPath); const inputFiles = await glob('**', inputPath);
const nowJsonRef = inputFiles['now.json']; const nowJsonRef = inputFiles['vercel.json'] || inputFiles['now.json'];
expect(nowJsonRef).toBeDefined(); expect(nowJsonRef).toBeDefined();
const nowJson = require(nowJsonRef.fsPath); const nowJson = require(nowJsonRef.fsPath);
expect(nowJson.builds.length).toBe(1); expect(nowJson.builds.length).toBe(1);