Skip to content

Commit 4836b2a

Browse files
committed
feat: added proper tests
1 parent 8510ca1 commit 4836b2a

10 files changed

+716
-238
lines changed

package-lock.json

+75
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"uuid": "^11.0.5"
4242
},
4343
"devDependencies": {
44+
"@fast-check/jest": "^1.1.0",
4445
"@swc/core": "^1.3.62",
4546
"@swc/jest": "^0.2.26",
4647
"@types/jest": "^28.1.3",
@@ -51,6 +52,7 @@
5152
"eslint-config-prettier": "^8.5.0",
5253
"eslint-plugin-import": "^2.26.0",
5354
"eslint-plugin-prettier": "^4.0.0",
55+
"fast-check": "^3.0.1",
5456
"jest": "^28.1.1",
5557
"jest-extended": "^3.0.1",
5658
"jest-junit": "^14.0.0",

src/Generator.ts

+8-24
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@ import * as constants from './constants';
66

77
// Computes the checksum by summing up all the bytes in the header
88
function computeChecksum(header: Uint8Array): number {
9-
if (!header.slice(148, 156).every((byte) => byte === 32)) {
10-
throw new errors.ErrorVirtualTarInvalidHeader(
11-
'Checksum field is not properly initialized with spaces',
12-
);
13-
}
149
return header.reduce((sum, byte) => sum + byte, 0);
1510
}
1611

17-
function createHeader(
12+
function generateHeader(
1813
filePath: string,
19-
stat: FileStat,
2014
type: EntryType,
15+
stat: FileStat,
2116
): Uint8Array {
2217
// TODO: implement long-file-name headers
2318
if (filePath.length < 1 || filePath.length > 255) {
@@ -26,14 +21,6 @@ function createHeader(
2621
);
2722
}
2823

29-
// The file path must not contain any directories, and must only contain a
30-
// file name. This guard checks that.
31-
if (filePath.includes('/')) {
32-
throw new errors.ErrorVirtualTarInvalidFileName(
33-
'File name must not contain /',
34-
);
35-
}
36-
3724
// As the size does not matter for directories, it can be undefined. However,
3825
// if the header is being generated for a file, then it needs to have a valid
3926
// size. This guard checks that.
@@ -202,14 +189,11 @@ function createHeader(
202189
return header;
203190
}
204191

205-
// Creates blocks marking the ned of the header. Returns one buffer of 1024
206-
// bytes filled with nulls. This aligns with the tar end-of-archive marker
207-
// being two null-filled blocks.
208-
function generateEndMarker() {
209-
return [
210-
new Uint8Array(constants.BLOCK_SIZE),
211-
new Uint8Array(constants.BLOCK_SIZE),
212-
];
192+
// Creates a single null block. A null block is a block filled with all zeros.
193+
// This is needed to end the archive, as two of these blocks mark the end of
194+
// archive.
195+
function generateNullChunk() {
196+
return new Uint8Array(constants.BLOCK_SIZE);
213197
}
214198

215-
export { createHeader, generateEndMarker };
199+
export { generateHeader, generateNullChunk };

0 commit comments

Comments
 (0)