Skip to content

Commit c01eac6

Browse files
committed
covers all the requirements in readme file
covers readme file
1 parent 643fefa commit c01eac6

File tree

5 files changed

+121
-4
lines changed

5 files changed

+121
-4
lines changed

implement-shell-tools/cat/cat.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ function printFile(filePath, options) {
88
const content = fs.readFileSync(filePath, "utf-8");
99
const lines = content.split("\n");
1010

11+
if (lines.length && lines[lines.length - 1] === "") {
12+
lines.pop();
13+
}
14+
1115
lines.forEach((line) => {
1216
let prefix = "";
1317

@@ -24,6 +28,7 @@ function printFile(filePath, options) {
2428
});
2529
} catch (error) {
2630
console.error(`cat: ${filePath}: ${error.message}`);
31+
process.exitCode = 1;
2732
}
2833
}
2934

@@ -51,6 +56,9 @@ function main() {
5156
}
5257

5358
files.forEach((file) => {
59+
if(options.numberMode) {
60+
globalLineCounter = 1; //reset line counter for each file if numbering is enabled
61+
}
5462
printFile(file, options);
5563
});
5664
}

implement-shell-tools/ls/ls.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ function listDirectory(dirPath, showAll, onePerLine) {
2020
}
2121
function main() {
2222
const args = process.argv.slice(2);
23-
// Check for options
2423
const showAll = args.includes("-a");
2524
const onePerLine = args.includes("-1");
2625
//remove options from args
2726
let directories = args.filter((arg) => arg !== "-a" && arg !== "-1");
2827

29-
// Default to current directory if no directories are specified
3028
if (directories.length === 0) {
3129
directories = [process.cwd()];
3230
}
@@ -35,7 +33,6 @@ function main() {
3533
const stats = fs.statSync(arg);
3634

3735
if (stats.isDirectory()) {
38-
//Print header if multiple directories are listed
3936
if (directories.length > 1) {
4037
console.log(`${arg}:`)
4138
};

implement-shell-tools/wc/wc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
const fs = require("node:fs");
33
// Function to count lines, words, and bytes in a file
44
function countFileContent(content) {
5-
const lines = content.split("\n").length; // Count lines by splitting on newline characters
5+
const rawLines = content.split("\n");
6+
const lines = rawLines[rawLines.length - 1] === "" ? rawLines.length - 1 : rawLines.length;
67
const words = content.trim().split(/\s+/).filter(Boolean).length; // Split by whitespace and filter out empty strings
78
const bytes = Buffer.byteLength(content, "utf8");
89
return { lines, words, bytes };

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "module-tools",
3+
"version": "1.0.0",
4+
"description": "Exercises to practice and solidify your understanding of the Tools module of the Software Development Course.",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "tests"
8+
},
9+
"scripts": {
10+
"test": "node tests/runIntegrationTests.js"
11+
},
12+
"keywords": [],
13+
"author": "",
14+
"license": "ISC"
15+
}

tests/runIntegrationTests.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const { execSync } = require("node:child_process");
2+
3+
function runCommand(command) {
4+
return execSync(command, { encoding: "utf-8" }).trim();
5+
}
6+
7+
function normalizeOutput(output) {
8+
return output.replaceAll(/\s+/g, " ").trim();
9+
}
10+
11+
function test(name, systemCommand, nodeCommand) {
12+
try {
13+
const expected = normalizeOutput(runCommand(systemCommand));
14+
const actual = normalizeOutput(runCommand(nodeCommand));
15+
if (expected === actual) {
16+
console.log(`✅ ${name} passed`);
17+
} else {
18+
console.error(`❌ ${name} failed`);
19+
console.error(`Expected: "${expected}"`);
20+
console.error(`Actual: "${actual}"`);
21+
}
22+
} catch (error) {
23+
console.error(`❌ ${name} failed with error: ${error.message}`);
24+
}
25+
}
26+
27+
/* CAT TESTS */
28+
test(
29+
"cat single file",
30+
"cat implement-shell-tools/cat/sample-files/1.txt",
31+
"node implement-shell-tools/cat/cat.js implement-shell-tools/cat/sample-files/1.txt",
32+
);
33+
34+
test(
35+
"cat -n single file",
36+
"cat -n implement-shell-tools/cat/sample-files/1.txt",
37+
"node implement-shell-tools/cat/cat.js -n implement-shell-tools/cat/sample-files/1.txt",
38+
);
39+
40+
test(
41+
"cat multiple files",
42+
"cat implement-shell-tools/cat/sample-files/*.txt",
43+
"node implement-shell-tools/cat/cat.js implement-shell-tools/cat/sample-files/*.txt",
44+
);
45+
46+
test(
47+
"cat -n multiple files",
48+
"cat -n implement-shell-tools/cat/sample-files/*.txt",
49+
"node implement-shell-tools/cat/cat.js -n implement-shell-tools/cat/sample-files/*.txt",
50+
);
51+
52+
test(
53+
"cat -b file",
54+
"cat -b implement-shell-tools/cat/sample-files/3.txt",
55+
"node implement-shell-tools/cat/cat.js -b implement-shell-tools/cat/sample-files/3.txt",
56+
);
57+
58+
/* LS TESTS */
59+
60+
test(
61+
"ls sample-files",
62+
"ls implement-shell-tools/ls/sample-files",
63+
"node implement-shell-tools/ls/ls.js implement-shell-tools/ls/sample-files",
64+
);
65+
66+
/* WC TESTS */
67+
68+
test(
69+
"wc all files",
70+
"wc implement-shell-tools/wc/sample-files/*",
71+
"node implement-shell-tools/wc/wc.js implement-shell-tools/wc/sample-files/*",
72+
);
73+
74+
test(
75+
"wc -l single file",
76+
"wc -l implement-shell-tools/wc/sample-files/3.txt",
77+
"node implement-shell-tools/wc/wc.js -l implement-shell-tools/wc/sample-files/3.txt",
78+
);
79+
80+
test(
81+
"wc -w single file",
82+
"wc -w implement-shell-tools/wc/sample-files/3.txt",
83+
"node implement-shell-tools/wc/wc.js -w implement-shell-tools/wc/sample-files/3.txt",
84+
);
85+
86+
test(
87+
"wc -c single file",
88+
"wc -c implement-shell-tools/wc/sample-files/3.txt",
89+
"node implement-shell-tools/wc/wc.js -c implement-shell-tools/wc/sample-files/3.txt",
90+
);
91+
92+
test(
93+
"wc -l multiple files",
94+
"wc -l implement-shell-tools/wc/sample-files/*",
95+
"node implement-shell-tools/wc/wc.js -l implement-shell-tools/wc/sample-files/*",
96+
);

0 commit comments

Comments
 (0)