Skip to content

Commit 7e543b0

Browse files
Refactor tests (#30)
* Renamed node.js.yml to build.yml * Moved test files 1. Moved `tests/1/` and `tests/2/` to `src/1/__tests__/` and `src/2/__tests__/` respectively 2. Updated relative imports 3. Updated test file paths * Moved test files for challenge 3, updated README for challenge 3 * Updated readme for challenge 1 and challenge 2 * Moved tests for challenge 4. Updated readme for challenge 4 * Updated readme and tests for challenge 5 * Updated tests and readme for challenge 6 * Updated tests and readme for challenge 7 * Updated tests and readme for challenge 8 * Updated test files and readme for challenge 9 * Updated tests and readme for challenge 10 * Updated readme and tests for challenge 11 * Updated tests and readme for challenge 13 * Updated readme and tests for challenge 14 * Updated tests and readme for challenge 15 * Updated tests and readme for challenge 16 * Updated readme and tests for challenge 17. Updated jest config file * Updated comments for challenge 1 tests. Updated global readme. * Updated tests for grep. Using LINUX path systems everywhere. * Updated path to shell.js, added console statements in cat.test.ts * Fixed challenge 14 and 15, updated readme for challenge 19 The challenges were referring to the `.js` file present in `./build/src` folders. Since the `tests` folder was removed, now there is no `src` folder present and we can directly using `./build` folder to refer to the `.js` files
1 parent 1c32c20 commit 7e543b0

File tree

117 files changed

+161
-141
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+161
-141
lines changed
File renamed without changes.

README.md

+3-3

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
roots: ['<rootDir>/src', '<rootDir>/tests'],
2+
roots: ['<rootDir>/src'],
33
testMatch: [
44
'**/__tests__/**/*.+(ts|tsx|js)',
55
'**/?(*.)+(spec|test).+(ts|tsx|js)'

src/1/README.md

+2-2
File renamed without changes.
File renamed without changes.

tests/1/wc.test.ts src/1/__tests__/wc.test.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { execSync } from 'child_process';
22
import fs from 'fs';
3-
import { myWC } from '../../src/1/wc';
3+
import { myWC } from '../wc';
4+
import path from 'path';
45

5-
const filePaths = ['./tests/1/test1.txt', './tests/1/test2.txt'];
6+
const filePaths = [
7+
path.join(__dirname, 'test1.txt'),
8+
path.join(__dirname, 'test2.txt')
9+
];
610

711
describe('Testing with filenames', () => {
812
filePaths.forEach((filePath) => {
@@ -11,7 +15,7 @@ describe('Testing with filenames', () => {
1115
.trim()
1216
.replace(/ +(?= )/g, '')
1317
.split(' ');
14-
// Sample output - 0 0 0 ./tests/1/test1.txt
18+
// Sample output - 0 0 0 ./src/1/__tests__/test1.txt
1519

1620
const l = output[0];
1721
const w = output[1];
@@ -57,7 +61,7 @@ describe('Testing without filename', () => {
5761
.trim()
5862
.replace(/ +(?= )/g, '')
5963
.split(' ');
60-
// Sample output - 0 0 0 ./tests/1/test1.txt
64+
// Sample output - 0 0 0 ./src/1/__tests__/test1.txt
6165

6266
const l = output[0];
6367
const w = output[1];

src/10/README.md

+2-2
File renamed without changes.
File renamed without changes.

tests/10/uniq.test.ts src/10/__tests__/uniq.test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs';
22
import { execSync } from 'child_process';
3-
import { uniq } from '../../src/10/uniq';
3+
import { uniq } from '../uniq';
4+
import path from 'path';
45

56
function expectLineByLine(buffer: Buffer, output: string) {
67
const expectedOutput = buffer
@@ -16,7 +17,10 @@ function expectLineByLine(buffer: Buffer, output: string) {
1617
}
1718

1819
describe('Testing with files', () => {
19-
const files = ['./tests/10/countries.txt', './tests/10/test.txt'];
20+
const files = [
21+
path.join(__dirname, 'countries.txt'),
22+
path.join(__dirname, 'test.txt')
23+
];
2024

2125
files.forEach((file) => {
2226
test(`Handle default behavior ${file}`, async () => {
@@ -58,7 +62,7 @@ describe('Testing with files', () => {
5862
});
5963

6064
describe('Testing with streams', () => {
61-
const file = './tests/10/test.txt';
65+
const file = path.join(__dirname, 'test.txt');
6266
const stream = fs.createReadStream(file);
6367

6468
test(`Handle default behavior ${file}`, async () => {

src/11/README.md

+1-3

tests/11/webserver.test.ts src/11/__tests__/webserver.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios';
2-
import { createWebServer } from '../../src/11';
3-
import { HttpServer } from '../../src/11/webserver';
2+
import { createWebServer } from '..';
3+
import { HttpServer } from '../webserver';
44

55
describe('Testing WebServer', () => {
66
const server: HttpServer = createWebServer(undefined, 8000);

src/13/README.md

+1-3

tests/13/diff.test.ts src/13/__tests__/diff.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { lcs, lcsArray } from '../../src/13/diff';
1+
import { lcs, lcsArray } from '../diff';
22

33
describe('Testing lcs Function', () => {
44
// [s1, s2]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/14/README.md

+4-2

tests/14/shell.test.ts src/14/__tests__/shell.test.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
import { ChildProcessWithoutNullStreams, execSync, spawn } from 'child_process';
2-
import path from 'path';
32

43
describe('Testing normal command execution', () => {
54
const inputs = [
6-
'uniq --count ./tests/10/countries.txt\n',
7-
'uniq --repeated ./tests/10/test.txt\n',
5+
'uniq --count ./src/10/__tests__/countries.txt\n',
6+
'uniq --repeated ./src/10/__tests__/test.txt\n',
87
'ls\n',
98
'cat README.md\n',
109
'cat README.md | wc\n',
1110
'cat README.md | wc | wc\n',
1211
'pwd\n',
13-
'cut -f2 -d, ./tests/4/fourchords.csv | uniq | wc -l\n'
12+
'cut -f2 -d, ./src/4/__tests__/fourchords.csv | uniq | wc -l\n'
1413
];
1514

1615
// File path to the shell.js file relative to this test file
17-
const filePath = path.join(
18-
__dirname,
19-
'..',
20-
'..',
21-
'build',
22-
'src',
23-
'14',
24-
'shell.js'
25-
);
16+
const filePath = './build/14/shell.js';
2617

2718
let shell: ChildProcessWithoutNullStreams;
2819

src/15/README.md

+2-2

tests/15/cat.test.ts src/15/__tests__/cat.test.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ChildProcessWithoutNullStreams, execSync, spawn } from 'child_process';
2+
import path from 'path';
23

34
/**
45
* This is a helper function which waits for the program to finish,
@@ -15,8 +16,12 @@ function catToolStdoutChecker(
1516
done: jest.DoneCallback,
1617
debug: boolean = false
1718
) {
18-
catTool.on('error', () => {});
19-
catTool.stderr.on('data', () => {});
19+
catTool.on('error', (err) => {
20+
console.error(err);
21+
});
22+
catTool.stderr.on('data', (data) => {
23+
console.error(data.toString());
24+
});
2025
let finalData = '';
2126

2227
catTool.stdout.on('data', (data) => {
@@ -32,11 +37,13 @@ function catToolStdoutChecker(
3237
});
3338
}
3439

40+
const pathToCatJs = './build/15/cat.js';
41+
3542
describe('Testing cat tool', () => {
3643
let catTool: ChildProcessWithoutNullStreams;
37-
const testFile1 = './tests/15/test1.txt';
38-
const testFile2 = './tests/15/test2.txt';
39-
const testFile3 = './tests/15/test3.txt';
44+
const testFile1 = path.join(__dirname, 'test1.txt');
45+
const testFile2 = path.join(__dirname, 'test2.txt');
46+
const testFile3 = path.join(__dirname, 'test3.txt');
4047

4148
afterEach(() => {
4249
catTool.kill('SIGINT');
@@ -45,15 +52,15 @@ describe('Testing cat tool', () => {
4552
it('Should output when provided a valid file', (done) => {
4653
const expectedOutput = execSync('cat ' + testFile1).toString();
4754

48-
catTool = spawn('node', ['./build/src/15/cat.js', testFile1]);
55+
catTool = spawn('node', [pathToCatJs, testFile1]);
4956

5057
catToolStdoutChecker(catTool, expectedOutput, done);
5158
});
5259

5360
it('Should handle input from stdin', (done) => {
5461
const expectedOutput = execSync(`head -n1 ${testFile1} | cat -`).toString();
5562

56-
catTool = spawn('node', ['./build/src/15/cat.js', '-']);
63+
catTool = spawn('node', [pathToCatJs, '-']);
5764

5865
catToolStdoutChecker(catTool, expectedOutput, done);
5966

@@ -64,7 +71,7 @@ describe('Testing cat tool', () => {
6471
it('Should concatenate files', (done) => {
6572
const expectedOutput = execSync(`cat ${testFile1} ${testFile2}`).toString();
6673

67-
catTool = spawn('node', ['./build/src/15/cat.js', testFile1, testFile2]);
74+
catTool = spawn('node', [pathToCatJs, testFile1, testFile2]);
6875

6976
catToolStdoutChecker(catTool, expectedOutput, done);
7077
});
@@ -76,7 +83,7 @@ describe('Testing cat tool', () => {
7683
.split(/\r\n|\n/)
7784
.join('\r\n');
7885

79-
catTool = spawn('node', ['./build/src/15/cat.js', '-n']);
86+
catTool = spawn('node', [pathToCatJs, '-n']);
8087

8188
catToolStdoutChecker(catTool, expectedOutput, done);
8289

@@ -91,7 +98,7 @@ describe('Testing cat tool', () => {
9198
.split(/\r\n|\n/)
9299
.join('\r\n');
93100

94-
catTool = spawn('node', ['./build/src/15/cat.js', '-b']);
101+
catTool = spawn('node', [pathToCatJs, '-b']);
95102

96103
catToolStdoutChecker(catTool, expectedOutput, done);
97104

File renamed without changes.
File renamed without changes.
File renamed without changes.

src/16/README.md

+1-3

tests/16/irc-client-error-handling.test.ts src/16/__tests__/irc-client-error-handling.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createLogger, format, transports } from 'winston';
2-
import IRCClient from '../../src/16/irc-client';
3-
import { IRCClientInterface } from '../../src/16/types';
2+
import IRCClient from '../irc-client';
3+
import { IRCClientInterface } from '../types';
44
import path from 'path';
55

66
const host = 'irc.freenode.net';

tests/16/irc-client-normal-commands.test.ts src/16/__tests__/irc-client-normal-commands.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path';
22
import { createLogger, format, transports } from 'winston';
3-
import { IRCClientInterface } from '../../src/16/types';
4-
import IRCClient from '../../src/16/irc-client';
3+
import { IRCClientInterface } from '../types';
4+
import IRCClient from '../irc-client';
55

66
const host = 'irc.freenode.net';
77
const port = 6667;

tests/16/irc-client-two-clients.test.ts src/16/__tests__/irc-client-two-clients.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLogger, format, transports } from 'winston';
2-
import IRCClient from '../../src/16/irc-client';
2+
import IRCClient from '../irc-client';
33
import path from 'path';
4-
import { IRCClientInterface } from '../../src/16/types';
4+
import { IRCClientInterface } from '../types';
55

66
const host = 'irc.freenode.net';
77
const port = 6667;

tests/16/parser.test.ts src/16/__tests__/parser.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IRCParser } from '../../src/16/parser';
1+
import { IRCParser } from '../parser';
22

33
describe('Testing IRC Parser', () => {
44
const validInputs = [

src/17/README.md

+1-3

tests/17/memcached-commands.test.ts src/17/__tests__/memcached-commands.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import MemCached from 'memcached';
22

3-
import MemCachedServer from '../../src/17/memcached';
4-
import sleep from '../../src/utils/sleep';
3+
import MemCachedServer from '../memcached';
4+
import sleep from '../../utils/sleep';
55
import { randomBytes } from 'crypto';
66

77
describe('Testing set and get commands', () => {

tests/17/memcached-concurrent-clients.test.ts src/17/__tests__/memcached-concurrent-clients.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import MemCached from 'memcached';
2-
import MemCachedServer from '../../src/17/memcached';
2+
import MemCachedServer from '../memcached';
33

44
describe('Handling concurrent clients', () => {
55
const key1 = 'test1',

src/19/README.md

+1-1

src/2/README.md

+2-2

tests/2/json_checker.test.ts src/2/__tests__/json_checker.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import fs from 'fs';
2-
import { JsonParser } from '../../src/2/json-parser';
2+
import { JsonParser } from '../json-parser';
3+
import path from 'path';
34

45
describe('Tests provided by JSON ORG', () => {
5-
const dir = './tests/2/json_checker/';
6+
const dir = path.join(__dirname, 'json_checker/');
67
const files = fs.readdirSync(dir);
78

89
afterEach(() => {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/2/step1.test.ts src/2/__tests__/step1.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import fs from 'fs';
2-
import { JsonParser } from '../../src/2/json-parser';
2+
import { JsonParser } from '../json-parser';
3+
import path from 'path';
34

45
describe('Step 1 tests', () => {
5-
const dir = './tests/2/step1/';
6+
const dir = path.join(__dirname, 'step1/');
67
const files = fs.readdirSync(dir);
78

89
afterEach(() => {
File renamed without changes.
File renamed without changes.

tests/2/step2.test.ts src/2/__tests__/step2.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import fs from 'fs';
2-
import { JsonParser } from '../../src/2/json-parser';
2+
import { JsonParser } from '../json-parser';
3+
import path from 'path';
34

45
describe('Step 2 tests', () => {
5-
const dir = './tests/2/step2/';
6+
const dir = path.join(__dirname, 'step2/');
67
const files = fs.readdirSync(dir);
78

89
afterEach(() => {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)