-
Notifications
You must be signed in to change notification settings - Fork 533
/
analytics-sources.spec.ts
208 lines (177 loc) · 6.95 KB
/
analytics-sources.spec.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {
getIntegrationName,
getIntegrationVersion,
getIntegrationEnvironment,
getIntegrationEnvironmentVersion,
INTEGRATION_NAME_ENVVAR,
INTEGRATION_VERSION_ENVVAR,
INTEGRATION_ENVIRONMENT_ENVVAR,
INTEGRATION_ENVIRONMENT_VERSION_ENVVAR,
isScoop,
isHomebrew,
validateHomebrew,
validateScoopManifestFile,
} from '../src/lib/analytics-sources';
import * as fs from 'fs';
const emptyArgs = [];
const defaultArgsParams = {
_: [],
_doubleDashArgs: [],
};
beforeEach(() => {
delete process.env[INTEGRATION_NAME_ENVVAR];
delete process.env[INTEGRATION_VERSION_ENVVAR];
delete process.env[INTEGRATION_ENVIRONMENT_ENVVAR];
delete process.env[INTEGRATION_ENVIRONMENT_VERSION_ENVVAR];
});
describe('analytics-sources - scoop detection', () => {
it('detects if snyk is installed via scoop', () => {
const originalExecPath = process.execPath;
process.execPath =
process.cwd() + '/test/fixtures/scoop/good-manifest/snyk-win.exe';
expect(isScoop()).toBe(true);
process.execPath = '/test/fixtures/scoop/bad-manifest/snyk-win.exe';
expect(isScoop()).toBe(false);
process.execPath = originalExecPath;
});
it('validates scoop manifest file', () => {
let snykExecPath =
process.cwd() + '/test/fixtures/scoop/good-manifest/snyk-win.exe';
expect(validateScoopManifestFile(snykExecPath)).toBe(true);
snykExecPath =
process.cwd() + '/test/fixtures/scoop/bad-manifest/snyk-win.exe';
expect(validateScoopManifestFile(snykExecPath)).toBe(false);
snykExecPath = process.cwd() + '/test/fixtures/scoop/no-exist/snyk-win.exe';
expect(validateScoopManifestFile(snykExecPath)).toBe(false);
});
});
describe('analytics-sources - Homebrew detection', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('detects if snyk is installed via Homebrew', () => {
const originalExecPath = process.execPath;
process.execPath = process.cwd() + '/usr/local/Cellar/snyk/v1.413.2/bin';
const fileExistsSpy = jest.spyOn(fs, 'existsSync').mockReturnValue(true);
expect(isHomebrew()).toBe(true);
expect(fileExistsSpy).toHaveBeenCalledTimes(1);
process.execPath = originalExecPath;
});
it('returns false if formula not in Homebrew path', () => {
const originalExecPath = process.execPath;
process.execPath =
process.cwd() + '/usr/local/some-other-location/snyk/v1.413.2/bin';
const fileExistsSpy = jest.spyOn(fs, 'existsSync');
expect(isHomebrew()).toBe(false);
expect(fileExistsSpy).not.toHaveBeenCalled();
process.execPath = originalExecPath;
});
it('returns false if formula files does not exist', () => {
const originalExecPath = process.execPath;
process.execPath = process.cwd() + '/usr/local/Cellar/snyk/v1.413.2/bin';
const fileExistsSpy = jest.spyOn(fs, 'existsSync').mockReturnValue(false);
expect(isHomebrew()).toBe(false);
expect(fileExistsSpy).toHaveBeenCalledTimes(1);
process.execPath = originalExecPath;
});
it('validates Homebrew formula file exists', () => {
let snykExecPath =
process.cwd() + '/test/fixtures/homebrew/Cellar/snyk/vX/bin/snyk'; // relies on fixture at /test/fixtures/homebrew/Cellar/vX/.brew/snyk.rb
expect(validateHomebrew(snykExecPath)).toBe(true);
snykExecPath =
process.cwd() +
'/test/fixtures/homebrew/no-exist/Cellar/snyk/vX/bin/snyk';
expect(validateHomebrew(snykExecPath)).toBe(false);
});
});
describe('analytics-sources - getIntegrationName', () => {
it('integration name is empty by default', () => {
expect(getIntegrationName(emptyArgs)).toBe('');
});
it('integration name is loaded from envvar', () => {
process.env[INTEGRATION_NAME_ENVVAR] = 'NPM';
expect(getIntegrationName(emptyArgs)).toBe('NPM');
process.env[INTEGRATION_NAME_ENVVAR] = 'STANDALONE';
expect(getIntegrationName(emptyArgs)).toBe('STANDALONE');
});
it('integration name is empty when envvar is not recognized', () => {
process.env[INTEGRATION_NAME_ENVVAR] = 'INVALID';
expect(getIntegrationName(emptyArgs)).toBe('');
});
it('integration name is loaded and formatted from CLI flag', () => {
expect(
getIntegrationName([
{ integrationName: 'homebrew', ...defaultArgsParams },
]),
).toBe('HOMEBREW');
});
it('integration name is loaded and validated from CLI flag', () => {
expect(
getIntegrationName([
{ integrationName: 'invalid', ...defaultArgsParams },
]),
).toBe('');
});
it('integration name SCOOP when snyk is installed with scoop', () => {
const originalExecPath = process.execPath;
process.execPath =
process.cwd() + '/test/fixtures/scoop/good-manifest/snyk-win.exe';
expect(getIntegrationName(emptyArgs)).toBe('SCOOP');
process.execPath = originalExecPath;
});
it('integration name HOMEBREW when snyk is installed with Homebrew', () => {
const originalExecPath = process.execPath;
process.execPath =
process.cwd() + '/test/fixtures/homebrew/Cellar/snyk/vX/bin/snyk'; // relies on fixture at /test/fixtures/homebrew/Cellar/vX/.brew/snyk.rb
expect(getIntegrationName(emptyArgs)).toBe('HOMEBREW');
process.execPath = originalExecPath;
});
});
describe('analytics-sources - getIntegrationVersion', () => {
it('integration version is empty by default', () => {
expect(getIntegrationVersion(emptyArgs)).toBe('');
});
it('integration version is loaded from envvar', () => {
process.env[INTEGRATION_VERSION_ENVVAR] = '1.2.3';
expect(getIntegrationVersion(emptyArgs)).toBe('1.2.3');
});
it('integration version is loaded from CLI flag', () => {
expect(
getIntegrationVersion([
{ integrationVersion: '1.2.3-Crystal', ...defaultArgsParams },
]),
).toBe('1.2.3-Crystal');
});
});
describe('analytics-sources - getIntegrationEnvironment', () => {
it('integration environment is empty by default', () => {
expect(getIntegrationEnvironment(emptyArgs)).toBe(undefined);
});
it('integration environment is loaded from envvar', () => {
process.env[INTEGRATION_ENVIRONMENT_ENVVAR] = 'WebStorm';
expect(getIntegrationEnvironment(emptyArgs)).toBe('WebStorm');
});
it('integration environment is loaded from CLI flag', () => {
expect(
getIntegrationEnvironment([
{ integrationEnvironment: 'PhpStorm', ...defaultArgsParams },
]),
).toBe('PhpStorm');
});
});
describe('analytics-sources - getIntegrationEnvironment', () => {
it('integration environment version is empty by default', () => {
expect(getIntegrationEnvironmentVersion(emptyArgs)).toBe(undefined);
});
it('integration environment version is loaded from envvar', () => {
process.env[INTEGRATION_ENVIRONMENT_VERSION_ENVVAR] = '2020.2';
expect(getIntegrationEnvironmentVersion(emptyArgs)).toBe('2020.2');
});
it('integration environment version is loaded from CLI flag', () => {
expect(
getIntegrationEnvironmentVersion([
{ integrationEnvironmentVersion: '7.0.0', ...defaultArgsParams },
]),
).toBe('7.0.0');
});
});