-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2149 from snyk/refactor/hotload
refactor: dynamically load cli modules
- Loading branch information
Showing
7 changed files
with
95 additions
and
159 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 |
---|---|---|
@@ -1,25 +1,28 @@ | ||
const abbrev = require('abbrev'); | ||
const hotload = require('../../lib/hotload')(__dirname); | ||
require('../../lib/spinner').isRequired = false; | ||
|
||
// the aim of this module is to load as little as possible to keep cli boot | ||
// time as low as possible | ||
// Wrapper for Commonjs compatibility | ||
async function callModule(mod, args) { | ||
const resolvedModule = await mod; | ||
return (resolvedModule.default || resolvedModule)(...args); | ||
} | ||
|
||
const commands = { | ||
auth: hotload('./auth'), | ||
config: hotload('./config'), | ||
help: hotload('./help'), | ||
ignore: hotload('./ignore'), | ||
modules: hotload('./modules'), | ||
monitor: hotload('./monitor'), | ||
fix: hotload('./fix'), | ||
policy: hotload('./policy'), | ||
protect: hotload('./protect'), | ||
test: hotload('./test'), | ||
version: hotload('./version'), | ||
wizard: hotload('./protect/wizard'), | ||
woof: hotload('./woof'), | ||
auth: async (...args) => callModule(import('./auth'), args), | ||
config: async (...args) => callModule(import('./config'), args), | ||
help: async (...args) => callModule(import('./help'), args), | ||
ignore: async (...args) => callModule(import('./ignore'), args), | ||
monitor: async (...args) => callModule(import('./monitor'), args), | ||
fix: async (...args) => callModule(import('./fix'), args), | ||
policy: async (...args) => callModule(import('./policy'), args), | ||
protect: async (...args) => callModule(import('./protect'), args), | ||
test: async (...args) => callModule(import('./test'), args), | ||
version: async (...args) => callModule(import('./version'), args), | ||
wizard: async (...args) => callModule(import('./protect/wizard'), args), | ||
woof: async (...args) => callModule(import('./woof'), args), | ||
}; | ||
|
||
commands.aliases = abbrev(Object.keys(commands)); | ||
commands.aliases.t = 'test'; | ||
|
||
module.exports = commands; |
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
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,88 +1,58 @@ | ||
import { | ||
chdirWorkspaces, | ||
getWorkspaceJSON, | ||
} from '../../acceptance/workspace-helper'; | ||
import { fakeServer } from '../../acceptance/fake-server'; | ||
import cli = require('../../../src/cli/commands'); | ||
import { chdir } from 'process'; | ||
import { createProjectFromWorkspace } from '../util/createProject'; | ||
import { runSnykCLI } from '../util/runSnykCLI'; | ||
|
||
describe('test using OAuth token', () => { | ||
let oldkey: string; | ||
let oldendpoint: string; | ||
const apiKey = '123456789'; | ||
const port: string = process.env.PORT || process.env.SNYK_PORT || '12345'; | ||
|
||
const BASE_API = '/api/v1'; | ||
|
||
const server = fakeServer(BASE_API, apiKey); | ||
|
||
const noVulnsResult = getWorkspaceJSON( | ||
'fail-on', | ||
'no-vulns', | ||
'vulns-result.json', | ||
); | ||
|
||
let origCwd: string; | ||
|
||
beforeAll(async () => { | ||
origCwd = process.cwd(); | ||
process.env.SNYK_API = `http://localhost:${port}${BASE_API}`; | ||
process.env.SNYK_HOST = `http://localhost:${port}`; | ||
|
||
let key = await cli.config('get', 'api'); | ||
oldkey = key; | ||
|
||
key = await cli.config('get', 'endpoint'); | ||
oldendpoint = key; | ||
|
||
await new Promise((resolve) => { | ||
server.listen(port, resolve); | ||
}); | ||
let server: ReturnType<typeof fakeServer>; | ||
let env: Record<string, string>; | ||
|
||
beforeAll((done) => { | ||
const apiPath = '/api/v1'; | ||
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345'; | ||
env = { | ||
...process.env, | ||
SNYK_API: 'http://localhost:' + apiPort + apiPath, | ||
SNYK_TOKEN: '123456789', | ||
SNYK_OAUTH_TOKEN: 'oauth-jwt-token', | ||
}; | ||
|
||
server = fakeServer(apiPath, env.SNYK_TOKEN); | ||
server.listen(apiPort, () => done()); | ||
}); | ||
|
||
afterAll(async () => { | ||
delete process.env.SNYK_API; | ||
delete process.env.SNYK_HOST; | ||
delete process.env.SNYK_PORT; | ||
delete process.env.SNYK_OAUTH_TOKEN; | ||
|
||
await server.close(); | ||
let key = 'set'; | ||
let value = `api=${oldkey}`; | ||
if (!oldkey) { | ||
key = 'unset'; | ||
value = 'api'; | ||
} | ||
await cli.config(key, value); | ||
if (oldendpoint) { | ||
await cli.config('endpoint', oldendpoint); | ||
} | ||
chdir(origCwd); | ||
}); | ||
|
||
it('successfully tests a project with an OAuth env variable set', async () => { | ||
process.env.SNYK_OAUTH_TOKEN = 'oauth-jwt-token'; | ||
const project = await createProjectFromWorkspace('fail-on/no-vulns'); | ||
const jsonObj = JSON.parse(await project.read('vulns-result.json')); | ||
server.setNextResponse(jsonObj); | ||
|
||
server.setNextResponse(noVulnsResult); | ||
chdirWorkspaces('fail-on'); | ||
await cli.test('no-vulns', { | ||
json: true, | ||
const { code } = await runSnykCLI(`test --json`, { | ||
cwd: project.path(), | ||
env, | ||
}); | ||
const req = server.popRequest(); | ||
expect(req.headers.authorization).toBe('Bearer oauth-jwt-token'); | ||
expect(req.method).toBe('POST'); | ||
|
||
expect(code).toEqual(0); | ||
const requests = server.popRequests(2); | ||
expect(requests[0].headers.authorization).toBe('Bearer oauth-jwt-token'); | ||
expect(requests[0].method).toBe('POST'); | ||
}); | ||
|
||
it('successfully monitors a project with an OAuth env variable set', async () => { | ||
process.env.SNYK_OAUTH_TOKEN = 'oauth-jwt-token'; | ||
const project = await createProjectFromWorkspace('fail-on/no-vulns'); | ||
const jsonObj = JSON.parse(await project.read('vulns-result.json')); | ||
server.setNextResponse(jsonObj); | ||
|
||
server.setNextResponse(noVulnsResult); | ||
chdirWorkspaces('fail-on'); | ||
await cli.monitor('no-vulns', { | ||
json: true, | ||
const { code } = await runSnykCLI(`monitor --json`, { | ||
cwd: project.path(), | ||
env, | ||
}); | ||
const req = server.popRequest(); | ||
expect(req.headers.authorization).toBe('Bearer oauth-jwt-token'); | ||
expect(req.method).toBe('PUT'); | ||
|
||
expect(code).toEqual(0); | ||
const requests = server.popRequests(2); | ||
expect(requests[0].headers.authorization).toBe('Bearer oauth-jwt-token'); | ||
expect(requests[0].method).toBe('PUT'); | ||
}); | ||
}); |
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
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
Oops, something went wrong.