|
1 | | -// // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | | -// // Licensed under the MIT License. |
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as path from 'path'; |
| 5 | +import * as os from 'os'; |
| 6 | +import { writeTestIdsFile } from '../../../client/testing/testController/common/utils'; |
| 7 | +import { EXTENSION_ROOT_DIR } from '../../../client/constants'; |
3 | 8 |
|
4 | | -// import * as assert from 'assert'; |
5 | | -// import { |
6 | | -// JSONRPC_CONTENT_LENGTH_HEADER, |
7 | | -// JSONRPC_CONTENT_TYPE_HEADER, |
8 | | -// JSONRPC_UUID_HEADER, |
9 | | -// ExtractJsonRPCData, |
10 | | -// parseJsonRPCHeadersAndData, |
11 | | -// splitTestNameWithRegex, |
12 | | -// argKeyExists, |
13 | | -// addValueIfKeyNotExist, |
14 | | -// } from '../../../client/testing/testController/common/utils'; |
| 9 | +suite('writeTestIdsFile tests', () => { |
| 10 | + let sandbox: sinon.SinonSandbox; |
15 | 11 |
|
16 | | -// suite('Test Controller Utils: JSON RPC', () => { |
17 | | -// test('Empty raw data string', async () => { |
18 | | -// const rawDataString = ''; |
| 12 | + setup(() => { |
| 13 | + sandbox = sinon.createSandbox(); |
| 14 | + }); |
19 | 15 |
|
20 | | -// const output = parseJsonRPCHeadersAndData(rawDataString); |
21 | | -// assert.deepStrictEqual(output.headers.size, 0); |
22 | | -// assert.deepStrictEqual(output.remainingRawData, ''); |
23 | | -// }); |
| 16 | + teardown(() => { |
| 17 | + sandbox.restore(); |
| 18 | + }); |
24 | 19 |
|
25 | | -// test('Valid data empty JSON', async () => { |
26 | | -// const rawDataString = `${JSONRPC_CONTENT_LENGTH_HEADER}: 2\n${JSONRPC_CONTENT_TYPE_HEADER}: application/json\n${JSONRPC_UUID_HEADER}: 1234\n\n{}`; |
| 20 | + test('should write test IDs to a temporary file', async () => { |
| 21 | + const testIds = ['test1', 'test2', 'test3']; |
| 22 | + const writeFileStub = sandbox.stub(fs.promises, 'writeFile').resolves(); |
27 | 23 |
|
28 | | -// const rpcHeaders = parseJsonRPCHeadersAndData(rawDataString); |
29 | | -// assert.deepStrictEqual(rpcHeaders.headers.size, 3); |
30 | | -// assert.deepStrictEqual(rpcHeaders.remainingRawData, '{}'); |
31 | | -// const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData); |
32 | | -// assert.deepStrictEqual(rpcContent.extractedJSON, '{}'); |
33 | | -// }); |
| 24 | + const result = await writeTestIdsFile(testIds); |
34 | 25 |
|
35 | | -// test('Valid data NO JSON', async () => { |
36 | | -// const rawDataString = `${JSONRPC_CONTENT_LENGTH_HEADER}: 0\n${JSONRPC_CONTENT_TYPE_HEADER}: application/json\n${JSONRPC_UUID_HEADER}: 1234\n\n`; |
| 26 | + const tmpDir = os.tmpdir(); |
37 | 27 |
|
38 | | -// const rpcHeaders = parseJsonRPCHeadersAndData(rawDataString); |
39 | | -// assert.deepStrictEqual(rpcHeaders.headers.size, 3); |
40 | | -// assert.deepStrictEqual(rpcHeaders.remainingRawData, ''); |
41 | | -// const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData); |
42 | | -// assert.deepStrictEqual(rpcContent.extractedJSON, ''); |
43 | | -// }); |
| 28 | + assert.ok(result.startsWith(tmpDir)); |
44 | 29 |
|
45 | | -// test('Valid data with full JSON', async () => { |
46 | | -// // this is just some random JSON |
47 | | -// const json = |
48 | | -// '{"jsonrpc": "2.0", "method": "initialize", "params": {"processId": 1234, "rootPath": "/home/user/project", "rootUri": "file:///home/user/project", "capabilities": {}}, "id": 0}'; |
49 | | -// const rawDataString = `${JSONRPC_CONTENT_LENGTH_HEADER}: ${json.length}\n${JSONRPC_CONTENT_TYPE_HEADER}: application/json\n${JSONRPC_UUID_HEADER}: 1234\n\n${json}`; |
| 30 | + assert.ok(writeFileStub.calledOnceWith(sinon.match.string, testIds.join('\n'))); |
| 31 | + }); |
50 | 32 |
|
51 | | -// const rpcHeaders = parseJsonRPCHeadersAndData(rawDataString); |
52 | | -// assert.deepStrictEqual(rpcHeaders.headers.size, 3); |
53 | | -// assert.deepStrictEqual(rpcHeaders.remainingRawData, json); |
54 | | -// const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData); |
55 | | -// assert.deepStrictEqual(rpcContent.extractedJSON, json); |
56 | | -// }); |
| 33 | + test('should handle error when accessing temp directory', async () => { |
| 34 | + const testIds = ['test1', 'test2', 'test3']; |
| 35 | + const error = new Error('Access error'); |
| 36 | + const accessStub = sandbox.stub(fs.promises, 'access').rejects(error); |
| 37 | + const writeFileStub = sandbox.stub(fs.promises, 'writeFile').resolves(); |
| 38 | + const mkdirStub = sandbox.stub(fs.promises, 'mkdir').resolves(); |
57 | 39 |
|
58 | | -// test('Valid data with multiple JSON', async () => { |
59 | | -// const json = |
60 | | -// '{"jsonrpc": "2.0", "method": "initialize", "params": {"processId": 1234, "rootPath": "/home/user/project", "rootUri": "file:///home/user/project", "capabilities": {}}, "id": 0}'; |
61 | | -// const rawDataString = `${JSONRPC_CONTENT_LENGTH_HEADER}: ${json.length}\n${JSONRPC_CONTENT_TYPE_HEADER}: application/json\n${JSONRPC_UUID_HEADER}: 1234\n\n${json}`; |
62 | | -// const rawDataString2 = rawDataString + rawDataString; |
| 40 | + const result = await writeTestIdsFile(testIds); |
63 | 41 |
|
64 | | -// const rpcHeaders = parseJsonRPCHeadersAndData(rawDataString2); |
65 | | -// assert.deepStrictEqual(rpcHeaders.headers.size, 3); |
66 | | -// const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData); |
67 | | -// assert.deepStrictEqual(rpcContent.extractedJSON, json); |
68 | | -// assert.deepStrictEqual(rpcContent.remainingRawData, rawDataString); |
69 | | -// }); |
| 42 | + const tempFileFolder = path.join(EXTENSION_ROOT_DIR, '.temp'); |
70 | 43 |
|
71 | | -// test('Valid constant', async () => { |
72 | | -// const data = `{"cwd": "/Users/eleanorboyd/testingFiles/inc_dec_example", "status": "success", "result": {"test_dup_class.test_a.TestSomething.test_a": {"test": "test_dup_class.test_a.TestSomething.test_a", "outcome": "success", "message": "None", "traceback": null, "subtest": null}}}`; |
73 | | -// const secondPayload = `Content-Length: 270 |
74 | | -// Content-Type: application/json |
75 | | -// Request-uuid: 496c86b1-608f-4886-9436-ec00538e144c |
| 44 | + assert.ok(result.startsWith(tempFileFolder)); |
76 | 45 |
|
77 | | -// ${data}`; |
78 | | -// const payload = `Content-Length: 270 |
79 | | -// Content-Type: application/json |
80 | | -// Request-uuid: 496c86b1-608f-4886-9436-ec00538e144c |
81 | | - |
82 | | -// ${data}${secondPayload}`; |
83 | | - |
84 | | -// const rpcHeaders = parseJsonRPCHeadersAndData(payload); |
85 | | -// assert.deepStrictEqual(rpcHeaders.headers.size, 3); |
86 | | -// const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData); |
87 | | -// assert.deepStrictEqual(rpcContent.extractedJSON, data); |
88 | | -// assert.deepStrictEqual(rpcContent.remainingRawData, secondPayload); |
89 | | -// }); |
90 | | -// test('Valid content length as only header with carriage return', async () => { |
91 | | -// const payload = `Content-Length: 7 |
92 | | -// `; |
93 | | - |
94 | | -// const rpcHeaders = parseJsonRPCHeadersAndData(payload); |
95 | | -// assert.deepStrictEqual(rpcHeaders.headers.size, 1); |
96 | | -// const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData); |
97 | | -// assert.deepStrictEqual(rpcContent.extractedJSON, ''); |
98 | | -// assert.deepStrictEqual(rpcContent.remainingRawData, ''); |
99 | | -// }); |
100 | | -// test('Valid content length header with no value', async () => { |
101 | | -// const payload = `Content-Length:`; |
102 | | - |
103 | | -// const rpcHeaders = parseJsonRPCHeadersAndData(payload); |
104 | | -// const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData); |
105 | | -// assert.deepStrictEqual(rpcContent.extractedJSON, ''); |
106 | | -// assert.deepStrictEqual(rpcContent.remainingRawData, ''); |
107 | | -// }); |
108 | | - |
109 | | -// suite('Test Controller Utils: Other', () => { |
110 | | -// interface TestCase { |
111 | | -// name: string; |
112 | | -// input: string; |
113 | | -// expectedParent: string; |
114 | | -// expectedSubtest: string; |
115 | | -// } |
116 | | - |
117 | | -// const testCases: Array<TestCase> = [ |
118 | | -// { |
119 | | -// name: 'Single parameter, named', |
120 | | -// input: 'test_package.ClassName.test_method (param=value)', |
121 | | -// expectedParent: 'test_package.ClassName.test_method', |
122 | | -// expectedSubtest: '(param=value)', |
123 | | -// }, |
124 | | -// { |
125 | | -// name: 'Single parameter, unnamed', |
126 | | -// input: 'test_package.ClassName.test_method [value]', |
127 | | -// expectedParent: 'test_package.ClassName.test_method', |
128 | | -// expectedSubtest: '[value]', |
129 | | -// }, |
130 | | -// { |
131 | | -// name: 'Multiple parameters, named', |
132 | | -// input: 'test_package.ClassName.test_method (param1=value1, param2=value2)', |
133 | | -// expectedParent: 'test_package.ClassName.test_method', |
134 | | -// expectedSubtest: '(param1=value1, param2=value2)', |
135 | | -// }, |
136 | | -// { |
137 | | -// name: 'Multiple parameters, unnamed', |
138 | | -// input: 'test_package.ClassName.test_method [value1, value2]', |
139 | | -// expectedParent: 'test_package.ClassName.test_method', |
140 | | -// expectedSubtest: '[value1, value2]', |
141 | | -// }, |
142 | | -// { |
143 | | -// name: 'Names with special characters', |
144 | | -// input: 'test_package.ClassName.test_method (param1=value/1, param2=value+2)', |
145 | | -// expectedParent: 'test_package.ClassName.test_method', |
146 | | -// expectedSubtest: '(param1=value/1, param2=value+2)', |
147 | | -// }, |
148 | | -// { |
149 | | -// name: 'Names with spaces', |
150 | | -// input: 'test_package.ClassName.test_method ["a b c d"]', |
151 | | -// expectedParent: 'test_package.ClassName.test_method', |
152 | | -// expectedSubtest: '["a b c d"]', |
153 | | -// }, |
154 | | -// ]; |
155 | | - |
156 | | -// testCases.forEach((testCase) => { |
157 | | -// test(`splitTestNameWithRegex: ${testCase.name}`, () => { |
158 | | -// const splitResult = splitTestNameWithRegex(testCase.input); |
159 | | -// assert.deepStrictEqual(splitResult, [testCase.expectedParent, testCase.expectedSubtest]); |
160 | | -// }); |
161 | | -// }); |
162 | | -// }); |
163 | | -// suite('Test Controller Utils: Args Mapping', () => { |
164 | | -// suite('addValueIfKeyNotExist', () => { |
165 | | -// test('should add key-value pair if key does not exist', () => { |
166 | | -// const args = ['key1=value1', 'key2=value2']; |
167 | | -// const result = addValueIfKeyNotExist(args, 'key3', 'value3'); |
168 | | -// assert.deepEqual(result, ['key1=value1', 'key2=value2', 'key3=value3']); |
169 | | -// }); |
170 | | - |
171 | | -// test('should not add key-value pair if key already exists', () => { |
172 | | -// const args = ['key1=value1', 'key2=value2']; |
173 | | -// const result = addValueIfKeyNotExist(args, 'key1', 'value3'); |
174 | | -// assert.deepEqual(result, ['key1=value1', 'key2=value2']); |
175 | | -// }); |
176 | | -// test('should not add key-value pair if key exists as a solo element', () => { |
177 | | -// const args = ['key1=value1', 'key2']; |
178 | | -// const result = addValueIfKeyNotExist(args, 'key2', 'yellow'); |
179 | | -// assert.deepEqual(result, ['key1=value1', 'key2']); |
180 | | -// }); |
181 | | -// test('add just key if value is null', () => { |
182 | | -// const args = ['key1=value1', 'key2']; |
183 | | -// const result = addValueIfKeyNotExist(args, 'key3', null); |
184 | | -// assert.deepEqual(result, ['key1=value1', 'key2', 'key3']); |
185 | | -// }); |
186 | | -// }); |
187 | | - |
188 | | -// suite('argKeyExists', () => { |
189 | | -// test('should return true if key exists', () => { |
190 | | -// const args = ['key1=value1', 'key2=value2']; |
191 | | -// const result = argKeyExists(args, 'key1'); |
192 | | -// assert.deepEqual(result, true); |
193 | | -// }); |
194 | | - |
195 | | -// test('should return false if key does not exist', () => { |
196 | | -// const args = ['key1=value1', 'key2=value2']; |
197 | | -// const result = argKeyExists(args, 'key3'); |
198 | | -// assert.deepEqual(result, false); |
199 | | -// }); |
200 | | -// }); |
201 | | -// }); |
202 | | -// }); |
| 46 | + assert.ok(accessStub.called); |
| 47 | + assert.ok(mkdirStub.called); |
| 48 | + assert.ok(writeFileStub.calledOnceWith(sinon.match.string, testIds.join('\n'))); |
| 49 | + }); |
| 50 | +}); |
0 commit comments