-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcbuild-idx-file.test.ts
More file actions
163 lines (137 loc) · 7.69 KB
/
cbuild-idx-file.test.ts
File metadata and controls
163 lines (137 loc) · 7.69 KB
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
/**
* Copyright 2025-2026 Arm Limited
*
* 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
*
* https://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 { it } from '@jest/globals';
import { TestDataHandler } from '../../__test__/test-data';
import path from 'node:path';
import * as fsUtils from '../../utils/fs-utils';
import { CbuildIdxFile } from './cbuild-idx-file';
import { ETextFileResult } from '../../generic/text-file';
import { getFileNameNoExt } from '../../utils/path-utils';
describe('CbuildIdxFile', () => {
const testDataHandler = new TestDataHandler();
let tmpSolutionDir: string;
beforeAll(async () => {
tmpSolutionDir = testDataHandler.copyTestDataToTmp('solutions');
});
afterAll(async () => {
testDataHandler.dispose();
});
it('test loading non-existing file', async () => {
const idxFile = new CbuildIdxFile();
const fileName = testDataHandler.tmpFileName('dummyFile.yml');
const loadResult = await idxFile.load(fileName);
expect(loadResult).toEqual(ETextFileResult.NotExists);
const root = idxFile.rootItem;
expect(root).toEqual(undefined);
expect(idxFile.activeContexts.length).toEqual(0);
});
it('test load existing file', async () => {
const solutionDir = path.join(tmpSolutionDir, 'USBD');
const fileName = path.join(solutionDir, 'USB_Device.cbuild-idx.yml');
const idxFile = new CbuildIdxFile();
let loadResult = await idxFile.load(fileName);
expect(loadResult).toEqual(ETextFileResult.Success);
expect(idxFile.cbuildLoadResult).toEqual(ETextFileResult.Success);
expect(idxFile.activeContexts.length).toEqual(2);
// load again: should be unchanged
loadResult = await idxFile.load(fileName);
expect(loadResult).toEqual(ETextFileResult.Unchanged);
expect(idxFile.cbuildLoadResult).toEqual(ETextFileResult.Unchanged);
expect(idxFile.activeContexts.length).toEqual(2);
expect(idxFile.activeContexts[0].displayName).toEqual('HID.Release+B-U585I-IOT02A');
expect(idxFile.activeContexts[0].buildType).toEqual('Release');
expect(idxFile.activeContexts[0].targetType).toEqual('B-U585I-IOT02A');
expect(idxFile.activeContexts[0].projectName).toEqual('HID');
const projectPath = path.join(solutionDir, 'HID', 'HID.cproject.yml');
expect(idxFile.activeContexts[0].projectPath).toEqual(projectPath);
expect(idxFile.activeContexts[0].layers).toBeDefined();
expect(idxFile.activeContexts[0].layers?.length).toEqual(2);
expect(idxFile.getContext('HID')).toEqual(idxFile.activeContexts[0]);
expect(idxFile.getContext('HID.cproject.yml')).toEqual(idxFile.activeContexts[0]);
expect(idxFile.getContext(projectPath)).toEqual(idxFile.activeContexts[0]);
const cgenPath = path.join(solutionDir, 'Board','B-U585I-IOT02A', 'CubeMX', 'Board.cgen.yml');
expect(idxFile.activeContexts[0].layers![0].absolutePath).toEqual(cgenPath);
expect(idxFile.activeContexts[0].layers![0].displayName).toEqual('Board');
const clayerPath = path.join(solutionDir, 'Board','B-U585I-IOT02A', 'Board.clayer.yml');
expect(idxFile.activeContexts[1].layers![1].absolutePath).toEqual(clayerPath);
expect(idxFile.activeContexts[1].layers![1].displayName).toEqual('Board');
expect(idxFile.activeContexts[1].displayName).toEqual('MassStorage.Debug+B-U585I-IOT02A');
});
it('test load modified cbuild file', async () => {
const solutionDir = path.join(tmpSolutionDir, 'USBD');
const fileName = path.join(solutionDir, 'USB_Device.cbuild-idx.yml');
const idxFile = new CbuildIdxFile();
let loadResult = await idxFile.load(fileName);
expect(loadResult).toEqual(ETextFileResult.Success);
expect(idxFile.cbuildLoadResult).toEqual(ETextFileResult.Success);
expect(idxFile.activeContexts.length).toEqual(2);
expect(idxFile.cbuildFiles.size).toEqual(2);
const cbuild = idxFile.cbuildFiles.get('HID');
expect(cbuild).toBeDefined();
cbuild?.clear();
// load again: should be changed
loadResult = await idxFile.load(fileName);
expect(loadResult).toEqual(ETextFileResult.Success);
expect(idxFile.cbuildLoadResult).toEqual(ETextFileResult.Success);
expect(idxFile.activeContexts.length).toEqual(2);
});
it('test load modified cbuild file with another context', async () => {
const solutionDir = path.join(tmpSolutionDir, 'USBD');
let fileName = path.join(solutionDir, 'USB_Device.cbuild-idx.yml');
const idxFile = new CbuildIdxFile();
let loadResult = await idxFile.load(fileName);
expect(loadResult).toEqual(ETextFileResult.Success);
expect(idxFile.cbuildLoadResult).toEqual(ETextFileResult.Success);
expect(idxFile.activeContexts.length).toEqual(2);
expect(idxFile.cbuildFiles.size).toEqual(2);
let cbuild = idxFile.cbuildFiles.get('HID');
expect(cbuild).toBeDefined();
let baseName = getFileNameNoExt(cbuild?.fileName);
expect(baseName).toEqual('HID.Release+B-U585I-IOT02A');
const cbuildItem = idxFile.topItem?.getChild('cbuilds')?.getChildByValue('cbuild', 'HID/HID.Release+B-U585I-IOT02A.cbuild.yml');
expect(cbuildItem).toBeDefined();
let value = cbuildItem?.getValue('cbuild');
cbuildItem?.setValue('cbuild', value?.replace('.Release+B-U585I-IOT02A', '.Debug+B-U585I-IOT02A'));
value = cbuildItem?.getValue('cbuild');
cbuildItem?.setValue('configuration', value?.replace('.Release+B-U585I-IOT02A', '.Debug+B-U585I-IOT02A'));
// save under another name to avoid interfering with other tests
fileName = fileName.replace('USB_Device.cbuild-idx.yml', 'USB_Device1.cbuild-idx.yml');
loadResult = await idxFile.save(fileName);
expect(loadResult).toEqual(ETextFileResult.Success);
// load again: should be changed because cbuild file is different
loadResult = await idxFile.load();
expect(loadResult).toEqual(ETextFileResult.Success);
expect(idxFile.cbuildLoadResult).toEqual(ETextFileResult.Success);
expect(idxFile.activeContexts.length).toEqual(2);
cbuild = idxFile.cbuildFiles.get('HID');
expect(cbuild).toBeDefined();
baseName = getFileNameNoExt(cbuild?.fileName);
expect(baseName).toEqual('HID.Debug+B-U585I-IOT02A');
});
it('test load non-existing cbuild file', async () => {
const solutionDir = path.join(tmpSolutionDir, 'USBD');
const fileName = path.join(solutionDir, 'USB_Device.cbuild-idx.yml');
const idxFile = new CbuildIdxFile();
jest.spyOn(fsUtils, 'fileExists').mockImplementation((filePath: string | undefined) =>!!filePath && !filePath.includes('MassStorage'));
const loadResult = await idxFile.load(fileName);
expect(loadResult).toEqual(ETextFileResult.NotExists);
expect(idxFile.cbuildLoadResult).toEqual(ETextFileResult.NotExists);
expect(idxFile.cbuildFiles.size).toEqual(2);
expect(idxFile.activeContexts.length).toEqual(2);
expect(idxFile.activeContexts[0].projectPath).toBeDefined();
expect(idxFile.activeContexts[1].projectPath).toBeUndefined();
});
});