@optimizely/ocp-cli 1.2.8 → 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,196 +0,0 @@
1
- import { AppUpdater } from '../lib/AppUpdater';
2
- import PackageManagerHandler from '../lib/packageManagerHandler';
3
-
4
- jest.mock('../lib/packageManagerHandler');
5
-
6
- describe('AppUpdater', () => {
7
- beforeEach(() => {
8
- jest.clearAllMocks();
9
- });
10
- afterEach(() => {
11
- jest.restoreAllMocks();
12
- });
13
- describe('getPackageDependencies', () => {
14
- it('should parse dependencies correctly', () => {
15
- const mockDeps = {
16
- type: 'tree',
17
- data: {
18
- type: 'list',
19
- trees: [
20
- {
21
- name: '@zaiusinc/package1@1.0.0',
22
- children: [],
23
- hint: null,
24
- color: 'bold',
25
- depth: 0,
26
- },
27
- {
28
- name: '@zaiusinc/package2@2.0.0',
29
- children: [],
30
- hint: null,
31
- color: 'bold',
32
- depth: 0,
33
- },
34
- ],
35
- },
36
- };
37
-
38
- (
39
- PackageManagerHandler.getPackageDependencies as jest.Mock
40
- ).mockReturnValueOnce([true, mockDeps]);
41
-
42
- const result = AppUpdater.getPackageDependencies();
43
-
44
- expect(result).toEqual({
45
- '@zaiusinc/package1': '1.0.0',
46
- '@zaiusinc/package2': '2.0.0',
47
- });
48
- });
49
-
50
- it('should handle prod dependencies flag', () => {
51
- const mockDeps = {
52
- type: 'tree',
53
- data: {
54
- type: 'list',
55
- trees: [
56
- {
57
- name: '@zaiusinc/package1@1.0.0',
58
- children: [],
59
- hint: null,
60
- color: 'bold',
61
- depth: 0,
62
- },
63
- ],
64
- },
65
- };
66
-
67
- const mockHandler = (
68
- PackageManagerHandler.getPackageDependencies as jest.Mock
69
- ).mockReturnValueOnce([true, mockDeps]);
70
-
71
- AppUpdater.getPackageDependencies(true);
72
-
73
- expect(mockHandler).toHaveBeenCalledWith(/@zaiusinc\//, true);
74
- });
75
-
76
- describe('error handling', () => {
77
- let mockExit: jest.SpyInstance;
78
- let mockConsole: jest.SpyInstance;
79
- let mockConsoleError: jest.SpyInstance;
80
- beforeEach(() => {
81
- mockExit = jest
82
- .spyOn(process, 'exit')
83
- .mockImplementation(() => undefined as never);
84
- mockConsole = jest
85
- .spyOn(console, 'log')
86
- .mockImplementation(() => undefined as never);
87
- mockConsoleError = jest
88
- .spyOn(console, 'error')
89
- .mockImplementation(() => undefined as never);
90
- });
91
-
92
- afterEach(() => {
93
- mockExit.mockRestore();
94
- mockConsole.mockRestore();
95
- mockConsoleError.mockRestore();
96
- });
97
-
98
- it('should exit with error on package manager failure', () => {
99
- (
100
- PackageManagerHandler.getPackageDependencies as jest.Mock
101
- ).mockReturnValueOnce([false, 'error']);
102
-
103
- AppUpdater.getPackageDependencies();
104
-
105
- expect(mockExit).toHaveBeenCalledWith(1);
106
- expect(mockConsoleError).toHaveBeenCalledWith('error');
107
- });
108
-
109
- it('should exit with error on exception', () => {
110
- (
111
- PackageManagerHandler.getPackageDependencies as jest.Mock
112
- ).mockImplementation(() => {
113
- throw new Error('test error');
114
- });
115
-
116
- AppUpdater.getPackageDependencies();
117
-
118
- expect(mockExit).toHaveBeenCalledWith(1);
119
- expect(mockConsoleError).toHaveBeenCalledWith(new Error('test error'));
120
- });
121
- });
122
- });
123
-
124
- describe('ensurePublicPackageUsage', () => {
125
- let mockExit: jest.SpyInstance;
126
- let mockConsoleLog: jest.SpyInstance;
127
- let mockConsoleError: jest.SpyInstance;
128
-
129
- beforeEach(() => {
130
- mockExit = jest
131
- .spyOn(process, 'exit')
132
- .mockImplementation(() => undefined as never);
133
- mockConsoleLog = jest
134
- .spyOn(console, 'log')
135
- .mockImplementation(() => undefined as never);
136
- mockConsoleError = jest
137
- .spyOn(console, 'error')
138
- .mockImplementation(() => undefined as never);
139
- });
140
-
141
- afterEach(() => {
142
- mockExit.mockRestore();
143
- mockConsoleLog.mockRestore();
144
- mockConsoleError.mockRestore();
145
- });
146
-
147
- it('should allow execution when no deprecated packages are found', () => {
148
- const mockDeps = {
149
- type: 'tree',
150
- data: {
151
- type: 'list',
152
- trees: [],
153
- },
154
- };
155
-
156
- (
157
- PackageManagerHandler.getPackageDependencies as jest.Mock
158
- ).mockReturnValueOnce([true, mockDeps]);
159
-
160
- AppUpdater.ensurePublicPackageUsage();
161
-
162
- expect(mockExit).not.toHaveBeenCalled();
163
- });
164
-
165
- it('should exit when deprecated @zaius packages are found', () => {
166
- const mockDeps = {
167
- type: 'tree',
168
- data: {
169
- type: 'list',
170
- trees: [
171
- {
172
- name: '@zaius/app-sdk@1.0.0',
173
- children: [],
174
- hint: null,
175
- color: 'bold',
176
- depth: 0,
177
- },
178
- ],
179
- },
180
- };
181
-
182
- (
183
- PackageManagerHandler.getPackageDependencies as jest.Mock
184
- ).mockReturnValueOnce([true, mockDeps]);
185
-
186
- AppUpdater.ensurePublicPackageUsage();
187
-
188
- expect(mockExit).toHaveBeenCalledWith(1);
189
- expect(mockConsoleLog).toHaveBeenCalledWith(
190
- expect.stringContaining(
191
- '@zaius/app-sdk & @zaius/node-sdk are no longer supported'
192
- )
193
- );
194
- });
195
- });
196
- });
@@ -1,190 +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 yarn when cli is installed with it', () => {
79
- (TerminalOutput.exec as jest.Mock)
80
- .mockReturnValueOnce({
81
- status: 0,
82
- stdout: '/foo/bar/yarn/bin/test-cli\n',
83
- stderr: '',
84
- }) // yarn check
85
- .mockReturnValueOnce({ status: 0, stdout: '1.0.0', stderr: '' }); // fetch command
86
-
87
- packageManager = new PackageManagerHandler('test-package', 'test-cli');
88
- const [success, version] = packageManager.fetchLatestPackage();
89
- expect(success).toBe(true);
90
- expect(version).toBe('1.0.0');
91
- });
92
- });
93
-
94
- describe('getPackageDependencies', () => {
95
- it('should parse npm dependencies into yarn-like format', () => {
96
- const mockNpmOutput = JSON.stringify({
97
- dependencies: {
98
- 'test-pkg': {
99
- version: '1.0.0',
100
- dependencies: {
101
- 'nested-test': {
102
- version: '2.0.0',
103
- },
104
- },
105
- },
106
- 'other-pkg': {
107
- version: '3.0.0',
108
- },
109
- },
110
- });
111
-
112
- (TerminalOutput.exec as jest.Mock).mockReturnValueOnce({
113
- status: 0,
114
- stdout: mockNpmOutput,
115
- stderr: '',
116
- }); // list command
117
-
118
- const [success, deps] =
119
- PackageManagerHandler.getPackageDependencies(/test/);
120
-
121
- expect(success).toBe(true);
122
- expect(deps).toEqual({
123
- type: 'tree',
124
- data: {
125
- type: 'list',
126
- trees: [
127
- {
128
- name: 'test-pkg@1.0.0',
129
- children: [],
130
- hint: null,
131
- color: 'bold',
132
- depth: 0,
133
- },
134
- {
135
- name: 'nested-test@2.0.0',
136
- children: [],
137
- hint: null,
138
- color: null,
139
- depth: 1,
140
- },
141
- ],
142
- },
143
- });
144
- });
145
- });
146
- describe('getPackageUpgradeCommand', () => {
147
- it('should return yarn upgrade command when cli is installed with yarn', () => {
148
- (TerminalOutput.exec as jest.Mock)
149
- .mockReturnValueOnce({
150
- status: 0,
151
- stdout: '/foo/bar/yarn/bin/test-cli\n',
152
- stderr: '',
153
- }) // yarn check
154
- .mockReturnValueOnce({ status: 0, stdout: '1.22.0', stderr: '' }); // check for yarn 1.x
155
-
156
- packageManager = new PackageManagerHandler('test-package', 'test-cli');
157
- const [success, command] = packageManager.getPackageUpgradeCommand();
158
- expect(success).toBe(true);
159
- expect(command).toBe('yarn global upgrade test-package --latest');
160
- });
161
-
162
- it('should return npm upgrade command when cli is installed with npm', () => {
163
- (TerminalOutput.exec as jest.Mock).mockReturnValueOnce({
164
- status: 0,
165
- stdout: '/usr/local/bin/test-cli\n',
166
- stderr: '',
167
- }); // npm check
168
-
169
- packageManager = new PackageManagerHandler('test-package', 'test-cli');
170
- const [success, command] = packageManager.getPackageUpgradeCommand();
171
- expect(success).toBe(true);
172
- expect(command).toBe('npm install --global test-package@latest');
173
- });
174
-
175
- it('should return error when yarn version is not 1.x', () => {
176
- (TerminalOutput.exec as jest.Mock)
177
- .mockReturnValueOnce({
178
- status: 0,
179
- stdout: '/foo/bar/yarn/bin/test-cli\n',
180
- stderr: '',
181
- }) // yarn check
182
- .mockReturnValueOnce({ status: 0, stdout: '2.22.0', stderr: '' }); // check for yarn 1.x
183
-
184
- packageManager = new PackageManagerHandler('test-package', 'test-cli');
185
- const [success, message] = packageManager.getPackageUpgradeCommand();
186
- expect(success).toBe(false);
187
- expect(message).toContain('not compatible');
188
- });
189
- });
190
- });