-
Notifications
You must be signed in to change notification settings - Fork 89
/
init.test.ts
116 lines (108 loc) · 3.87 KB
/
init.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright 2021 Salto Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Workspace } from '@salto-io/workspace'
import { locateWorkspaceRoot } from '@salto-io/core'
import * as mocks from '../mocks'
import { action } from '../../src/commands/init'
import { buildEventName } from '../../src/telemetry'
import { getEnvName } from '../../src/callbacks'
import { CommandArgs } from '../../src/command_builder'
jest.mock('@salto-io/core', () => ({
...jest.requireActual<{}>('@salto-io/core'),
initLocalWorkspace: jest.fn().mockImplementation(
(_baseDir: string, workspaceName: string): Workspace => {
if (workspaceName === 'error') throw new Error('failed')
return {
name: workspaceName,
uid: '',
currentEnv: () => 'default',
envs: () => ['default'],
} as unknown as Workspace
}
),
locateWorkspaceRoot: jest.fn(),
}))
const mockLocateWorkspaceRoot = (
locateWorkspaceRoot as jest.MockedFunction<typeof locateWorkspaceRoot>
)
jest.mock('../../src/callbacks', () => {
const actual = jest.requireActual('../../src/callbacks')
return {
...actual,
getEnvName: jest.fn().mockImplementation(actual.getEnvName),
}
})
const mockGetEnvName = getEnvName as jest.MockedFunction<typeof getEnvName>
const commandName = 'init'
const eventsNames = {
success: buildEventName(commandName, 'success'),
start: buildEventName(commandName, 'start'),
failure: buildEventName(commandName, 'failure'),
}
describe('init command', () => {
let cliCommandArgs: CommandArgs
let telemetry: mocks.MockTelemetry
let output: mocks.MockCliOutput
beforeEach(async () => {
mockGetEnvName.mockResolvedValue('default')
mockLocateWorkspaceRoot.mockResolvedValue(undefined)
const cliArgs = mocks.mockCliArgs()
cliCommandArgs = {
...mocks.mockCliCommandArgs(commandName, cliArgs),
workspacePath: '.',
}
telemetry = cliArgs.telemetry
output = cliArgs.output
})
it('should invoke api\'s init', async () => {
await action({
...cliCommandArgs,
input: {
workspaceName: 'test',
},
})
expect(output.stdout.content.search('test')).toBeGreaterThan(0)
expect(telemetry.getEvents()).toHaveLength(2)
expect(telemetry.getEventsMap()[eventsNames.failure]).toBeUndefined()
expect(telemetry.getEventsMap()[eventsNames.success]).not.toBeUndefined()
expect(telemetry.getEventsMap()[eventsNames.start]).not.toBeUndefined()
})
it('should print errors', async () => {
await action({
...cliCommandArgs,
input: {
workspaceName: 'error',
},
})
expect(output.stderr.content.search('failed')).toBeGreaterThan(0)
expect(telemetry.getEvents()).toHaveLength(2)
expect(telemetry.getEventsMap()[eventsNames.success]).toBeUndefined()
expect(telemetry.getEventsMap()[eventsNames.failure]).not.toBeUndefined()
expect(telemetry.getEventsMap()[eventsNames.start]).not.toBeUndefined()
})
it('should avoid initiating a workspace which already exists', async () => {
const path = '/some/path/to/workspace'
mockLocateWorkspaceRoot.mockResolvedValue(path)
await action({
...cliCommandArgs,
input: {
workspaceName: 'test',
},
})
expect(output.stderr.content).toEqual(`Could not initiate workspace: existing salto workspace in ${path}\n\n`)
expect(output.stdout.content).toEqual('')
})
})