Skip to content

Commit ba5f7b7

Browse files
committed
Added test coverage for saveConfigurationFile
1 parent d250c2b commit ba5f7b7

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

src/utils/ConfigUtils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ export async function saveConfigurationFile(configurationFile: ConfigurationFile
148148
}
149149
for (let entry of section.entries) {
150150
const comments = entry.commentLines.map(value => value.rawValue).join("\n")
151-
writeString += `${comments}\n`
151+
if (comments.length > 0) {
152+
writeString += `${comments}\n`
153+
}
152154
writeString += `${entry.entryName} = ${entry.value}\n\n`;
153155
}
154156
}

test/jest/__tests__/utils/utils.ConfigUtils.ts.spec.ts

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import FsProvider from '../../../../src/providers/generic/file/FsProvider';
22
import InMemoryFsProvider from '../stubs/providers/InMemory.FsProvider';
33
import {
44
buildConfigurationFileFromPath,
5-
ConfigurationEntry, ConfigurationEntryDisplayType,
6-
getSelectOptions
5+
ConfigurationEntry, ConfigurationEntryDisplayType, ConfigurationFile,
6+
getSelectOptions, saveConfigurationFile
77
} from '../../../../src/utils/ConfigUtils';
88
import path from 'path';
99

@@ -237,3 +237,89 @@ describe('getSelectOptions', () => {
237237

238238
});
239239

240+
describe('saveConfigurationFile', () => {
241+
242+
beforeAll(() => {
243+
const fsProvider = new InMemoryFsProvider();
244+
FsProvider.provide(() => fsProvider);
245+
});
246+
247+
beforeEach(() => {
248+
InMemoryFsProvider.clear();
249+
});
250+
251+
test('saves', async () => {
252+
const fileDirectory = path.join("config", "saved");
253+
const fileName = "configurationFileToSave.cfg";
254+
const configurationFile: ConfigurationFile = {
255+
filename: fileName,
256+
path: path.join(fileDirectory, fileName),
257+
sections: [
258+
{
259+
sectionName: "First Section",
260+
entries: [
261+
{
262+
entryName: "FirstSectionEntry",
263+
value: "Saved value",
264+
cachedValue: "Old value",
265+
displayType: 'input',
266+
commentLines: [],
267+
}
268+
]
269+
},
270+
{
271+
sectionName: "Second Section",
272+
entries: [
273+
{
274+
entryName: "SecondSectionFirstEntry",
275+
value: "First entry new value",
276+
cachedValue: "First entry old value",
277+
displayType: 'input',
278+
commentLines: [
279+
{
280+
rawValue: '## First comment line',
281+
displayValue: 'First comment line',
282+
isDescription: true,
283+
},
284+
{
285+
rawValue: '# Metadata comment line',
286+
displayValue: 'Metadata comment line',
287+
isDescription: false,
288+
},
289+
],
290+
},
291+
{
292+
entryName: "SecondSectionSecondEntry",
293+
value: "Second entry new value",
294+
cachedValue: "Second entry old value",
295+
displayType: 'input',
296+
commentLines: [],
297+
}
298+
]
299+
}
300+
]
301+
}
302+
303+
await FsProvider.instance.mkdirs(fileDirectory);
304+
await saveConfigurationFile(configurationFile);
305+
306+
const fileContent = (await FsProvider.instance.readFile(configurationFile.path)).toString();
307+
308+
expect(fileContent).toEqual(
309+
`[First Section]
310+
311+
FirstSectionEntry = Saved value
312+
313+
[Second Section]
314+
315+
## First comment line
316+
# Metadata comment line
317+
SecondSectionFirstEntry = First entry new value
318+
319+
SecondSectionSecondEntry = Second entry new value
320+
321+
`
322+
)
323+
})
324+
325+
});

0 commit comments

Comments
 (0)