Skip to content

Commit 09ec02c

Browse files
author
Simon Renoult
committed
refactor: stop using external git repo for tests
1 parent f809ee6 commit 09ec02c

File tree

6 files changed

+243
-93
lines changed

6 files changed

+243
-93
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"prepare": "npm run lint && npm run build",
2626
"postversion": "git push --tags",
2727
"pretest": "npm run lint",
28-
"test": "mocha --require ts-node/register test/**/*.ts",
28+
"test": "mocha --timeout=5000 --require ts-node/register test/**/*.ts",
2929
"test:ci": "nyc --reporter=lcov npm test"
3030
},
3131
"keywords": [

test/code-complexity-fixture

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { tmpdir } from "os";
2+
import { sep } from "path";
3+
import { existsSync, mkdirSync, rmdirSync } from "fs";
4+
import { execSync } from "child_process";
5+
import VersionedFileFixture from "./versioned-file.fixture";
6+
7+
export default class TestRepositoryFixture {
8+
private readonly systemTemporaryDirectory = tmpdir();
9+
public static readonly testRepositoryName = "code-complexity-test-directory";
10+
11+
public readonly location: string;
12+
private files: {
13+
name: string;
14+
commits?: number;
15+
lines?: number;
16+
date?: string;
17+
}[] = [];
18+
19+
constructor() {
20+
this.location = `${this.systemTemporaryDirectory}${sep}${TestRepositoryFixture.testRepositoryName}`;
21+
}
22+
23+
addFile(args: {
24+
name: string;
25+
commits?: number;
26+
lines?: number;
27+
date?: string;
28+
}): this {
29+
this.files.push(args);
30+
return this;
31+
}
32+
33+
public writeOnDisk(): this {
34+
if (existsSync(this.location)) {
35+
rmdirSync(this.location, { recursive: true });
36+
}
37+
mkdirSync(this.location);
38+
execSync(`git -C ${this.location} init`);
39+
40+
this.files.forEach((f) => {
41+
new VersionedFileFixture(this.location)
42+
.withName(f.name)
43+
.containing({ lines: f.lines || 1 })
44+
.committed({ times: f.commits || 1, date: f.date })
45+
.writeOnDisk();
46+
});
47+
48+
return this;
49+
}
50+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { execSync } from "child_process";
2+
import { appendFileSync, writeFileSync } from "fs";
3+
import { sep } from "path";
4+
5+
export default class VersionedFileFixture {
6+
constructor(private readonly repositoryLocation: string) {}
7+
8+
private name = "example.js";
9+
private numberOfLinesInFile = 10;
10+
private numberOfCommitsForFile = 10;
11+
private commitDate?: string;
12+
13+
withName(name: string): VersionedFileFixture {
14+
this.name = name;
15+
return this;
16+
}
17+
18+
containing(args: { lines: number }): VersionedFileFixture {
19+
this.numberOfLinesInFile = args.lines;
20+
return this;
21+
}
22+
23+
committed(args: { times: number; date?: string }): VersionedFileFixture {
24+
this.numberOfCommitsForFile = args.times;
25+
this.commitDate = args.date;
26+
return this;
27+
}
28+
29+
writeOnDisk(): void {
30+
for (let i = 0; i < this.numberOfCommitsForFile; i++) {
31+
if (i === 0) {
32+
this.createFileWithContentInRepository(this.name);
33+
this.addFileToRepository();
34+
} else {
35+
this.modifyFileWithoutChangingItsLength(i);
36+
}
37+
this.commitFile(i);
38+
}
39+
}
40+
41+
private commitFile(i: number): void {
42+
const commitMessage = `"${this.name}: commit #${i + 1}"`;
43+
const command = this.commitDate
44+
? `GIT_COMMITTER_DATE="${this.commitDate}" git -C ${this.repositoryLocation} commit --all --message=${commitMessage} --date=${this.commitDate}`
45+
: `git -C ${this.repositoryLocation} commit --all --message=${commitMessage}`;
46+
try {
47+
execSync(command);
48+
} catch (e) {
49+
console.log(e.stdout.toString());
50+
throw e;
51+
}
52+
}
53+
54+
private modifyFileWithoutChangingItsLength(i: number): void {
55+
appendFileSync(
56+
`${this.repositoryLocation}${sep}${this.name}`,
57+
`// change for commit #${i + 1} `
58+
);
59+
}
60+
61+
private createFileWithContentInRepository(fileName: string): void {
62+
writeFileSync(
63+
`${this.repositoryLocation}${sep}${fileName}`,
64+
new Array(this.numberOfLinesInFile)
65+
.fill(null)
66+
.map((value, index) => `console.log(${index});`)
67+
.join("\n")
68+
);
69+
}
70+
71+
private addFileToRepository(): void {
72+
execSync(`git -C ${this.repositoryLocation} add --all`);
73+
}
74+
}

0 commit comments

Comments
 (0)