Skip to content

Commit 95768cf

Browse files
committed
chapter 03: check the validity of the file extension in a disk file, that creates an external dependency, unit tests are broken
1 parent e57c8f1 commit 95768cf

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extensions": ["slf", "sql"]
3+
}

chapter_03-using-stubs-to-break-dependencies/LogAn/logAnalyzer.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
const fs = require('fs');
2+
const util = require('util');
13
const ArgumentError = require('./ArgumentError');
24

5+
const readFilePromisied = util.promisify(fs.readFile);
6+
37
function logAnalyzer() {
48
/**
59
* @type {boolean}
@@ -15,23 +19,51 @@ function logAnalyzer() {
1519

1620
/**
1721
* @param {string} fileName
18-
* @return {boolean}
22+
* @return {Promise}
1923
*/
20-
function isValidLogFileName(fileName) {
24+
async function isValidLogFileName(fileName) {
2125
wasLastFileNameValid = false;
2226

2327
if (fileName === '') {
2428
throw new ArgumentError('filename has to be provided');
2529
}
2630

27-
if (!fileName.toUpperCase().endsWith('.SLF')) {
31+
const result = await isValid(fileName);
32+
33+
if (!result) {
2834
return false;
2935
}
3036

3137
wasLastFileNameValid = true;
3238
return true;
3339
}
3440

41+
/**
42+
* @param {string} fileName
43+
*/
44+
async function isValid(fileName) {
45+
const fileNameExtensions = await readFilePromisied(
46+
`${__dirname}/fileNameExtensions.config.json`,
47+
'utf8'
48+
).then(fileContent => JSON.parse(fileContent).extensions);
49+
50+
const isValidExtension = fileNameExtensions.some(
51+
function checkFileNameExtension(extension) {
52+
if (
53+
fileName
54+
.toUpperCase()
55+
.endsWith(`.${extension.toUpperCase()}`)
56+
) {
57+
return true;
58+
}
59+
60+
return false;
61+
}
62+
);
63+
64+
return isValidExtension;
65+
}
66+
3567
return {
3668
getWasLastFileNameValid,
3769
isValidLogFileName,

0 commit comments

Comments
 (0)