Skip to content

Commit

Permalink
[now-cli] Add tests for alias rules (#3003)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyBitz authored and kodiakhq[bot] committed Sep 11, 2019
1 parent c944706 commit 64356ba
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 43 deletions.
111 changes: 68 additions & 43 deletions packages/now-cli/test/helpers/prepare.js
Expand Up @@ -85,14 +85,14 @@ const randomAliasSuffix = randomBytes(6).toString('hex');

const getRevertAliasConfigFile = () => {
return JSON.stringify({
'version': 2,
'name': `now-revert-alias-${randomAliasSuffix}`,
'builds': [
{
'src': '*.json',
'use': '@now/static'
}
]
version: 2,
name: `now-revert-alias-${randomAliasSuffix}`,
builds: [
{
src: '*.json',
use: '@now/static',
},
],
});
};

Expand All @@ -103,14 +103,14 @@ module.exports = async session => {
'package.json': getPackageFile(session),
'now.json': getConfigFile(false),
'first.png': getImageFile(session, {
size: 30
size: 30,
}),
'second.png': getImageFile(session, {
size: 20
size: 20,
}),
'now.json-builds': getConfigFile(true),
'index.html': getIndexHTMLFile(session),
'contact.php': getContactFile(session)
'contact.php': getContactFile(session),
};

const spec = {
Expand All @@ -120,26 +120,24 @@ module.exports = async session => {
'static-single-file': ['first.png', 'now.json'],
'static-multiple-files': ['first.png', 'second.png', 'now.json'],
'single-dotfile': {
'.testing': 'i am a dotfile'
'.testing': 'i am a dotfile',
},
'config-alias-property': {
'now.json':
'{ "alias": "test.now.sh", "builds": [ { "src": "*.html", "use": "@now/static" } ] }',
'index.html': '<span>test alias</span'
'index.html': '<span>test alias</span',
},
'config-scope-property-email': {
'now.json':
`{ "scope": "${session}@zeit.pub", "builds": [ { "src": "*.html", "use": "@now/static" } ], "version": 2 }`,
'index.html': '<span>test scope email</span'
'now.json': `{ "scope": "${session}@zeit.pub", "builds": [ { "src": "*.html", "use": "@now/static" } ], "version": 2 }`,
'index.html': '<span>test scope email</span',
},
'config-scope-property-username': {
'now.json':
`{ "scope": "${session}", "builds": [ { "src": "*.html", "use": "@now/static" } ] }`,
'index.html': '<span>test scope username</span'
'now.json': `{ "scope": "${session}", "builds": [ { "src": "*.html", "use": "@now/static" } ] }`,
'index.html': '<span>test scope username</span',
},
'builds-wrong': {
'now.json': '{"builder": 1, "type": "static"}',
'index.html': '<span>test</span'
'index.html': '<span>test</span',
},
'builds-no-list': {
'now.json': `{
Expand All @@ -161,75 +159,102 @@ module.exports = async session => {
FROM alpine
RUN mkdir /public
RUN echo hello > /public/index.html
`
`,
},
'build-env': {
'now.json': JSON.stringify({
version: 1,
type: 'static',
build: {
env: { FOO: 'bar' }
}
env: { FOO: 'bar' },
},
}),
Dockerfile: `
FROM alpine
ARG FOO
RUN mkdir /public
RUN echo $FOO > /public/index.html
`
`,
},
'build-env-arg': {
'now.json': JSON.stringify({
version: 1,
type: 'static'
type: 'static',
}),
Dockerfile: `
FROM alpine
ARG NONCE
RUN mkdir /public
RUN echo $NONCE > /public/index.html
`
`,
},
'now-revert-alias-1': {
'index.json': JSON.stringify({ name: 'now-revert-alias-1' }),
'now.json': getRevertAliasConfigFile()
'now.json': getRevertAliasConfigFile(),
},
'now-revert-alias-2': {
'index.json': JSON.stringify({ name: 'now-revert-alias-2' }),
'now.json': getRevertAliasConfigFile()
'now.json': getRevertAliasConfigFile(),
},
'now-dev-fail-dev-script': {
'package.json': JSON.stringify({
scripts: {
dev: 'now dev'
}
}, null, 2)
'package.json': JSON.stringify(
{
scripts: {
dev: 'now dev',
},
},
null,
2
),
},
'v1-warning-link': {
'now.json': JSON.stringify({
version: 1
version: 1,
}),
'package.json': JSON.stringify({
dependencies: {
next: '9.0.0'
}
})
next: '9.0.0',
},
}),
},
'static-deployment': {
'index.txt': 'Hello World'
'index.txt': 'Hello World',
},
'failing-build': {
'package.json': JSON.stringify({
scripts: {
build: 'echo hello && exit 1'
}
})
}
build: 'echo hello && exit 1',
},
}),
},
'alias-rules': {
'rules.json': JSON.stringify({
rules: [
// for example:
// { pathname: '/', dest: '' },
// { pathname: '/', dest: '', method: 'GET' }
// Will be generated by the actual test
],
}),
'invalid-rules.json': JSON.stringify({
what: { what: 0 },
}),
'invalid-type-rules.json': JSON.stringify({
rules: { what: 0 },
}),
'invalid-json-rules.json': '==ok',
},
};

for (const typeName of Object.keys(spec)) {
const needed = spec[typeName];
const directory = join(__dirname, '..', 'fixtures', 'integration', typeName);
const directory = join(
__dirname,
'..',
'fixtures',
'integration',
typeName
);
await mkdirp(directory);

if (Array.isArray(needed)) {
Expand Down
53 changes: 53 additions & 0 deletions packages/now-cli/test/integration.js
Expand Up @@ -405,6 +405,59 @@ test('deploy a dockerfile project', async t => {
context.deployment = host;
});

test('test invalid json alias rules', async t => {
const fixturePath = fixture('alias-rules');
const output = await execute(['alias', '-r', 'invalid-json-rules.json'], {
cwd: fixturePath,
});

t.is(output.code, 1, formatOutput(output));
t.regex(output.stderr, /Error parsing/, formatOutput(output));
});

test('test invalid alias rules', async t => {
const fixturePath = fixture('alias-rules');
const output = await execute(['alias', '-r', 'invalid-rules.json'], {
cwd: fixturePath,
});

t.is(output.code, 1, formatOutput(output));
t.regex(output.stderr, /Path Alias validation error/, formatOutput(output));
});

test('test invalid type for alias rules', async t => {
const fixturePath = fixture('alias-rules');
const output = await execute(['alias', '-r', 'invalid-type-rules.json'], {
cwd: fixturePath,
});

t.is(output.code, 1, formatOutput(output));
t.regex(output.stderr, /Path Alias validation error/, formatOutput(output));
});

test('apply alias rules', async t => {
const fixturePath = fixture('alias-rules');

// Create the rules file
const alias = `test-alias-rules.${contextName}.now.sh`;

const now = {
alias: alias,
};

const rules = {
rules: [{ pathname: '/docker-deployment', dest: context.deployment }],
};

await writeFile(path.join(fixturePath, 'now.json'), JSON.stringify(now));
await writeFile(path.join(fixturePath, 'rules.json'), JSON.stringify(rules));

const output = await execute(['alias', '-r', 'rules.json'], {
cwd: fixturePath,
});
t.is(output.code, 0, formatOutput(output));
});

test('find deployment in list', async t => {
const output = await execa(binaryPath, ['--debug', 'ls', ...defaultArgs], {
reject: false,
Expand Down

0 comments on commit 64356ba

Please sign in to comment.