Skip to content

Commit

Permalink
feat: added proper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Feb 27, 2025
1 parent 8510ca1 commit 4836b2a
Show file tree
Hide file tree
Showing 10 changed files with 716 additions and 238 deletions.
75 changes: 75 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"uuid": "^11.0.5"
},
"devDependencies": {
"@fast-check/jest": "^1.1.0",
"@swc/core": "^1.3.62",
"@swc/jest": "^0.2.26",
"@types/jest": "^28.1.3",
Expand All @@ -51,6 +52,7 @@
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.0.0",
"fast-check": "^3.0.1",
"jest": "^28.1.1",
"jest-extended": "^3.0.1",
"jest-junit": "^14.0.0",
Expand Down
32 changes: 8 additions & 24 deletions src/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ import * as constants from './constants';

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

function createHeader(
function generateHeader(
filePath: string,
stat: FileStat,
type: EntryType,
stat: FileStat,
): Uint8Array {
// TODO: implement long-file-name headers
if (filePath.length < 1 || filePath.length > 255) {
Expand All @@ -26,14 +21,6 @@ function createHeader(
);
}

// The file path must not contain any directories, and must only contain a
// file name. This guard checks that.
if (filePath.includes('/')) {
throw new errors.ErrorVirtualTarInvalidFileName(
'File name must not contain /',
);
}

// As the size does not matter for directories, it can be undefined. However,
// if the header is being generated for a file, then it needs to have a valid
// size. This guard checks that.
Expand Down Expand Up @@ -202,14 +189,11 @@ function createHeader(
return header;
}

// Creates blocks marking the ned of the header. Returns one buffer of 1024
// bytes filled with nulls. This aligns with the tar end-of-archive marker
// being two null-filled blocks.
function generateEndMarker() {
return [
new Uint8Array(constants.BLOCK_SIZE),
new Uint8Array(constants.BLOCK_SIZE),
];
// Creates a single null block. A null block is a block filled with all zeros.
// This is needed to end the archive, as two of these blocks mark the end of
// archive.
function generateNullChunk() {
return new Uint8Array(constants.BLOCK_SIZE);
}

export { createHeader, generateEndMarker };
export { generateHeader, generateNullChunk };
Loading

0 comments on commit 4836b2a

Please sign in to comment.