-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate tests needing the file system
- Loading branch information
1 parent
273cfbc
commit 7c4855e
Showing
3 changed files
with
157 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
exports.command = 'init'; | ||
exports.desc = 'Create a statickit.json file in the current directory'; | ||
exports.builder = {}; | ||
exports.handler = function(args) { | ||
console.log('init called'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,140 +1,121 @@ | ||
const execa = require('execa'); | ||
const fs = require('fs').promises; | ||
const path = require('path'); | ||
require('dotenv').config(); | ||
|
||
// Execute the `statickit` command from a `tmp` directory | ||
const command = async (args, opts) => { | ||
return await execa.command( | ||
`../bin/statickit ${args}`, | ||
Object.assign({ cwd: 'tmp' }, opts) | ||
); | ||
}; | ||
|
||
// Resolve path to file in the working directory for the command | ||
const resolvePath = file => { | ||
return path.resolve('tmp', file); | ||
}; | ||
|
||
beforeEach(async () => { | ||
// Create a temp working directory, since some of the tests | ||
// need to create .env files and config files that might | ||
// conflict with what's in the root of this repo. | ||
await fs.mkdir('tmp'); | ||
}); | ||
|
||
afterEach(async () => { | ||
const files = await fs.readdir('tmp'); | ||
|
||
// Delete all the files in the temp directory | ||
for (const file of files) { | ||
await fs.unlink(resolvePath(file)); | ||
} | ||
const { | ||
command, | ||
makeSandbox, | ||
removeSandbox, | ||
sandboxCommand, | ||
resolveSandbox | ||
} = require('./helpers'); | ||
|
||
describe('using file system', () => { | ||
beforeEach(async () => { | ||
await makeSandbox(); | ||
}); | ||
|
||
await fs.rmdir('tmp'); | ||
}); | ||
afterEach(async () => { | ||
await removeSandbox(); | ||
}); | ||
|
||
it('returns help output', async () => { | ||
const { stdout } = await command('--help'); | ||
expect(stdout).toMatch(/Performs a deployment/); | ||
}); | ||
it('returns an error if no config is present', async () => { | ||
try { | ||
await sandboxCommand('deploy'); | ||
} catch (result) { | ||
expect(result.exitCode).toBe(1); | ||
expect(result.stderr).toMatch(/Configuration not provided/); | ||
} | ||
}); | ||
|
||
it('returns an error if no config is present', async () => { | ||
try { | ||
await command('deploy'); | ||
} catch (result) { | ||
expect(result.exitCode).toBe(1); | ||
expect(result.stderr).toMatch(/Configuration not provided/); | ||
} | ||
}); | ||
it('accepts a deploy key from .env file', async () => { | ||
await fs.writeFile( | ||
resolveSandbox('.env'), | ||
`STATICKIT_DEPLOY_KEY=${process.env.STATICKIT_TEST_DEPLOY_KEY}`, | ||
'utf8' | ||
); | ||
|
||
it('returns an error if config is unparsable', async () => { | ||
try { | ||
await command(`deploy -c '{'`); | ||
} catch (result) { | ||
expect(result.exitCode).toBe(1); | ||
expect(result.stderr).toMatch(/Configuration could not be parsed/); | ||
} | ||
}); | ||
const { stdout, exitCode } = await sandboxCommand(`deploy -c '{}'`); | ||
expect(exitCode).toBe(0); | ||
expect(stdout).toMatch(/Deployment succeeded/); | ||
}); | ||
|
||
it('returns an error if deploy key is not found', async () => { | ||
try { | ||
await command(`deploy -c '{}'`); | ||
} catch (result) { | ||
expect(result.exitCode).toBe(1); | ||
expect(result.stderr).toMatch(/Deploy key not found/); | ||
} | ||
}); | ||
it('accepts a config from the statickit.json file', async () => { | ||
await fs.writeFile(resolveSandbox('statickit.json'), '{}', 'utf8'); | ||
|
||
it('returns an error if deploy key is invalid', async () => { | ||
try { | ||
await command(`deploy -c '{}' -k invalidkey`); | ||
} catch (result) { | ||
expect(result.exitCode).toBe(1); | ||
expect(result.stderr).toMatch(/Deploy key is not valid/); | ||
} | ||
}); | ||
const { stdout, exitCode } = await sandboxCommand('deploy', { | ||
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY } | ||
}); | ||
expect(exitCode).toBe(0); | ||
expect(stdout).toMatch(/Deployment succeeded/); | ||
}); | ||
|
||
it('succeeds given valid params', async () => { | ||
const { stdout, exitCode } = await command( | ||
`deploy -c '{}' -k ${process.env.STATICKIT_TEST_DEPLOY_KEY}` | ||
); | ||
expect(exitCode).toBe(0); | ||
expect(stdout).toMatch(/Deployment succeeded/); | ||
}); | ||
it('accepts a config from a custom file', async () => { | ||
await fs.writeFile(resolveSandbox('statickit-custom.json'), '{}', 'utf8'); | ||
|
||
it('accepts a deploy key from env', async () => { | ||
const { stdout, exitCode } = await command(`deploy -c '{}'`, { | ||
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY } | ||
const { stdout, exitCode } = await sandboxCommand( | ||
'deploy --file statickit-custom.json', | ||
{ | ||
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY } | ||
} | ||
); | ||
expect(exitCode).toBe(0); | ||
expect(stdout).toMatch(/Deployment succeeded/); | ||
}); | ||
expect(exitCode).toBe(0); | ||
expect(stdout).toMatch(/Deployment succeeded/); | ||
}); | ||
|
||
it('accepts a deploy key from .env file', async () => { | ||
await fs.writeFile( | ||
resolvePath('.env'), | ||
`STATICKIT_DEPLOY_KEY=${process.env.STATICKIT_TEST_DEPLOY_KEY}`, | ||
'utf8' | ||
); | ||
|
||
const { stdout, exitCode } = await command(`deploy -c '{}'`); | ||
expect(exitCode).toBe(0); | ||
expect(stdout).toMatch(/Deployment succeeded/); | ||
}); | ||
describe('using args', () => { | ||
it('returns an error if config is unparsable', async () => { | ||
try { | ||
await command(`deploy -c '{'`); | ||
} catch (result) { | ||
expect(result.exitCode).toBe(1); | ||
expect(result.stderr).toMatch(/Configuration could not be parsed/); | ||
} | ||
}); | ||
|
||
it('accepts a config from the statickit.json file', async () => { | ||
await fs.writeFile(resolvePath('statickit.json'), '{}', 'utf8'); | ||
it('returns an error if deploy key is not found', async () => { | ||
try { | ||
await command(`deploy -c '{}'`); | ||
} catch (result) { | ||
expect(result.exitCode).toBe(1); | ||
expect(result.stderr).toMatch(/Deploy key not found/); | ||
} | ||
}); | ||
|
||
const { stdout, exitCode } = await command('deploy', { | ||
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY } | ||
it('returns an error if deploy key is invalid', async () => { | ||
try { | ||
await command(`deploy -c '{}' -k invalidkey`); | ||
} catch (result) { | ||
expect(result.exitCode).toBe(1); | ||
expect(result.stderr).toMatch(/Deploy key is not valid/); | ||
} | ||
}); | ||
expect(exitCode).toBe(0); | ||
expect(stdout).toMatch(/Deployment succeeded/); | ||
}); | ||
|
||
it('accepts a config from a custom file', async () => { | ||
await fs.writeFile(resolvePath('statickit-custom.json'), '{}', 'utf8'); | ||
it('succeeds given valid params', async () => { | ||
const { stdout, exitCode } = await command( | ||
`deploy -c '{}' -k ${process.env.STATICKIT_TEST_DEPLOY_KEY}` | ||
); | ||
expect(exitCode).toBe(0); | ||
expect(stdout).toMatch(/Deployment succeeded/); | ||
}); | ||
|
||
const { stdout, exitCode } = await command( | ||
'deploy --file statickit-custom.json', | ||
{ | ||
it('accepts a deploy key from env', async () => { | ||
const { stdout, exitCode } = await command(`deploy -c '{}'`, { | ||
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY } | ||
} | ||
); | ||
expect(exitCode).toBe(0); | ||
expect(stdout).toMatch(/Deployment succeeded/); | ||
}); | ||
}); | ||
expect(exitCode).toBe(0); | ||
expect(stdout).toMatch(/Deployment succeeded/); | ||
}); | ||
|
||
it('fails given invalid params', async () => { | ||
try { | ||
await command( | ||
`deploy -c '{"forms":{"a":{"name":""}}}' -k ${process.env.STATICKIT_TEST_DEPLOY_KEY}` | ||
); | ||
} catch (result) { | ||
expect(result.exitCode).toBe(1); | ||
expect(result.stderr).toMatch( | ||
/Deployment failed due to configuration errors/ | ||
); | ||
} | ||
it('fails given invalid params', async () => { | ||
try { | ||
await command( | ||
`deploy -c '{"forms":{"a":{"name":""}}}' -k ${process.env.STATICKIT_TEST_DEPLOY_KEY}` | ||
); | ||
} catch (result) { | ||
expect(result.exitCode).toBe(1); | ||
expect(result.stderr).toMatch( | ||
/Deployment failed due to configuration errors/ | ||
); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require('dotenv').config(); | ||
|
||
const execa = require('execa'); | ||
const path = require('path'); | ||
const fs = require('fs').promises; | ||
|
||
// Execute the `statickit` command from the root directory | ||
// Tests that use this should not rely on the contents of the | ||
// file system (e.g. a statickit.json file). | ||
const command = async (args, opts) => { | ||
return await execa.command(`bin/statickit ${args}`, opts); | ||
}; | ||
|
||
// Execute the `statickit` command from the sandbox directory | ||
const sandboxCommand = async (args, opts) => { | ||
return await execa.command( | ||
`../bin/statickit ${args}`, | ||
Object.assign({ cwd: 'tmp' }, opts) | ||
); | ||
}; | ||
|
||
// Resolve path to file in the working directory for the command | ||
const resolveSandbox = file => { | ||
return path.resolve('tmp', file); | ||
}; | ||
|
||
// Create a sandbox directory to serve a the command working directory, | ||
// since tests that interact with the file system conflict with files | ||
// in the repo. | ||
const makeSandbox = async () => { | ||
await fs.mkdir('tmp'); | ||
}; | ||
|
||
const removeSandbox = async () => { | ||
const files = await fs.readdir('tmp'); | ||
|
||
// Delete all the files in the temp directory | ||
for (const file of files) { | ||
await fs.unlink(resolveSandbox(file)); | ||
} | ||
|
||
await fs.rmdir('tmp'); | ||
}; | ||
|
||
module.exports = { | ||
command, | ||
sandboxCommand, | ||
resolveSandbox, | ||
makeSandbox, | ||
removeSandbox | ||
}; |