@optimizely/ocp-cli 1.2.9 → 1.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,199 +0,0 @@
1
- /* tslint:disable:max-line-length */
2
- import PackageManagerHandler from '../lib/packageManagerHandler';
3
- import { TerminalOutput } from '../lib/TerminalOutput';
4
-
5
- jest.mock('../lib/TerminalOutput', () => ({
6
- TerminalOutput: {
7
- exec: jest.fn(),
8
- },
9
- }));
10
-
11
- describe('PackageManagerHandler', () => {
12
- let packageManager: PackageManagerHandler;
13
-
14
- beforeEach(() => {
15
- jest.clearAllMocks();
16
- });
17
-
18
- afterEach(() => {
19
- jest.restoreAllMocks();
20
- });
21
-
22
- describe('isLegacyYarnAvailable', () => {
23
- it('should return true when yarn 1.x is available', () => {
24
- (TerminalOutput.exec as jest.Mock).mockReturnValue({
25
- status: 0,
26
- stdout: '1.22.19\n',
27
- stderr: '',
28
- });
29
- expect(PackageManagerHandler.isLegacyYarnAvailable()).toBe(true);
30
- });
31
-
32
- it('should return false when yarn 2.x is available', () => {
33
- (TerminalOutput.exec as jest.Mock).mockReturnValue({
34
- status: 0,
35
- stdout: '2.0.0\n',
36
- stderr: '',
37
- });
38
- expect(PackageManagerHandler.isLegacyYarnAvailable()).toBe(false);
39
- });
40
- });
41
-
42
- describe('upgradeGlobalPackageToLatest', () => {
43
- it('should use yarn when the cli is installed using yarn', () => {
44
- (TerminalOutput.exec as jest.Mock)
45
- .mockReturnValueOnce({
46
- status: 0,
47
- stdout: '/foo/bar/yarn/bin/test-cli\n',
48
- stderr: '',
49
- }) // yarn check
50
- .mockReturnValueOnce({ status: 0, stdout: '1.22.0', stderr: '' }) // check for yarn 1.x
51
- .mockReturnValueOnce({ status: 0, stdout: 'success', stderr: '' }); // upgrade command
52
-
53
- packageManager = new PackageManagerHandler('test-package', 'test-cli');
54
- const [success, _] = packageManager.upgradeGlobalPackageToLatest();
55
- expect(success).toBe(true);
56
- expect(TerminalOutput.exec).toHaveBeenCalledWith(
57
- 'yarn global upgrade test-package --latest'
58
- );
59
- });
60
-
61
- it('should notify failure when the cli is installed using yarn 1.x but current yarn version is newer than 1.x', () => {
62
- (TerminalOutput.exec as jest.Mock)
63
- .mockReturnValueOnce({
64
- status: 0,
65
- stdout: '/foo/bar/yarn/bin/test-cli\n',
66
- stderr: '',
67
- }) // yarn check
68
- .mockReturnValueOnce({ status: 0, stdout: '2.22.0', stderr: '' }); // check for yarn 1.x
69
-
70
- packageManager = new PackageManagerHandler('test-package', 'test-cli');
71
- const [success, message] = packageManager.upgradeGlobalPackageToLatest();
72
- expect(success).toBe(false);
73
- expect(message).toContain('not compatible');
74
- });
75
- });
76
-
77
- describe('fetchLatestPackage', () => {
78
- it('should fetch latest version using --silent flag to avoid messing with the npm output', () => {
79
- (TerminalOutput.exec as jest.Mock).mockReturnValue({
80
- status: 0,
81
- stdout: '1.0.0',
82
- stderr: '',
83
- });
84
- packageManager = new PackageManagerHandler('test-package', 'test-cli');
85
- const [success, version] = packageManager.fetchLatestPackage();
86
- expect(success).toBe(true);
87
- expect(version).toBe('1.0.0');
88
- expect(TerminalOutput.exec).toHaveBeenCalledWith(
89
- expect.stringContaining('npm show --silent')
90
- );
91
- });
92
- });
93
-
94
- describe('getPackageDependencies', () => {
95
- let mockNpmOutput: string;
96
- beforeEach(() => {
97
- mockNpmOutput = JSON.stringify({
98
- dependencies: {
99
- 'test-pkg': {
100
- version: '1.0.0',
101
- dependencies: {
102
- 'nested-test': {
103
- version: '2.0.0',
104
- },
105
- },
106
- },
107
- 'other-pkg': {
108
- version: '3.0.0',
109
- },
110
- },
111
- });
112
-
113
- (TerminalOutput.exec as jest.Mock).mockReturnValue({
114
- status: 0,
115
- stdout: mockNpmOutput,
116
- stderr: '',
117
- }); // list command
118
- });
119
- it('should parse npm dependencies into yarn-like format', () => {
120
- const [success, deps] =
121
- PackageManagerHandler.getPackageDependencies(/test/);
122
-
123
- expect(success).toBe(true);
124
- expect(deps).toEqual({
125
- type: 'tree',
126
- data: {
127
- type: 'list',
128
- trees: [
129
- {
130
- name: 'test-pkg@1.0.0',
131
- children: [],
132
- hint: null,
133
- color: 'bold',
134
- depth: 0,
135
- },
136
- {
137
- name: 'nested-test@2.0.0',
138
- children: [],
139
- hint: null,
140
- color: null,
141
- depth: 1,
142
- },
143
- ],
144
- },
145
- });
146
- });
147
-
148
- it('should call the npm command with the --silent flag to avoid messing up with the ls output', () => {
149
- PackageManagerHandler.getPackageDependencies(/other/);
150
- expect(TerminalOutput.exec).toHaveBeenCalledWith(
151
- expect.stringContaining('npm ls --silent')
152
- );
153
- });
154
- });
155
- describe('getPackageUpgradeCommand', () => {
156
- it('should return yarn upgrade command when cli is installed with yarn', () => {
157
- (TerminalOutput.exec as jest.Mock)
158
- .mockReturnValueOnce({
159
- status: 0,
160
- stdout: '/foo/bar/yarn/bin/test-cli\n',
161
- stderr: '',
162
- }) // yarn check
163
- .mockReturnValueOnce({ status: 0, stdout: '1.22.0', stderr: '' }); // check for yarn 1.x
164
-
165
- packageManager = new PackageManagerHandler('test-package', 'test-cli');
166
- const [success, command] = packageManager.getPackageUpgradeCommand();
167
- expect(success).toBe(true);
168
- expect(command).toBe('yarn global upgrade test-package --latest');
169
- });
170
-
171
- it('should return npm upgrade command when cli is installed with npm', () => {
172
- (TerminalOutput.exec as jest.Mock).mockReturnValueOnce({
173
- status: 0,
174
- stdout: '/usr/local/bin/test-cli\n',
175
- stderr: '',
176
- }); // npm check
177
-
178
- packageManager = new PackageManagerHandler('test-package', 'test-cli');
179
- const [success, command] = packageManager.getPackageUpgradeCommand();
180
- expect(success).toBe(true);
181
- expect(command).toBe('npm install --global test-package@latest');
182
- });
183
-
184
- it('should return error when yarn version is not 1.x', () => {
185
- (TerminalOutput.exec as jest.Mock)
186
- .mockReturnValueOnce({
187
- status: 0,
188
- stdout: '/foo/bar/yarn/bin/test-cli\n',
189
- stderr: '',
190
- }) // yarn check
191
- .mockReturnValueOnce({ status: 0, stdout: '2.22.0', stderr: '' }); // check for yarn 1.x
192
-
193
- packageManager = new PackageManagerHandler('test-package', 'test-cli');
194
- const [success, message] = packageManager.getPackageUpgradeCommand();
195
- expect(success).toBe(false);
196
- expect(message).toContain('not compatible');
197
- });
198
- });
199
- });