Skip to content

Commit

Permalink
fix (cspell-tools): normalize .gz output (#4796)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Sep 1, 2023
1 parent c9ed003 commit bdb384b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
18 changes: 12 additions & 6 deletions packages/cspell-tools/src/app.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Commander from 'commander';
import * as fs from 'fs/promises';
import * as path from 'path';
import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';

import * as app from './app.js';
import { readTextFile } from './compiler/readers/readTextFile.js';
Expand All @@ -10,10 +10,14 @@ import { compressFile } from './gzip/compressFiles.js';
import { spyOnConsole } from './test/console.js';
import { createTestHelper } from './test/TestHelper.js';

vi.mock('./gzip/compressFiles.js', () => ({
compressFile: vi.fn().mockImplementation((name: string) => Promise.resolve(name + '.gz')),
OSFlags: { Unix: 3 },
}));
vi.mock('./gzip/compressFiles.js', async () => {
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const mod = await vi.importActual<typeof import('./gzip/compressFiles.js')>('./gzip/compressFiles.js');
return {
...mod,
compressFile: vi.fn().mockImplementation((name: string) => Promise.resolve(name + '.gz')),
};
});

const OSFlags = {
Unix: 3,
Expand Down Expand Up @@ -53,9 +57,11 @@ describe('Validate the application', () => {
beforeEach(() => {
testHelper.createTempDir();
testHelper.cp(path.join(pathSamples, 'cities.txt'), '.');
vi.resetAllMocks();
consoleSpy.attach();
});
afterEach(() => {
vi.resetAllMocks();
});

test('app compile-trie', async () => {
const commander = getCommander();
Expand Down
13 changes: 8 additions & 5 deletions packages/cspell-tools/src/compiler/readers/readTextFile.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import assert from 'assert';
import { promises as fs } from 'fs';
import { promisify } from 'util';
import * as zlib from 'zlib';

const gunzip = promisify(zlib.gunzip);
import { decompress } from '../../gzip/index.js';

const isGzFile = /\.gz$/;

export function readTextFile(filename: string): Promise<string> {
const content = fs
.readFile(filename)
.then((buffer) => (isGzFile.test(filename) ? gunzip(buffer) : buffer))
.then((buffer) => buffer.toString('utf8'));
.then(async (buffer) => (isGzFile.test(filename) ? decompress(buffer) : buffer))
.then((buffer) => (assertIsBuffer(buffer), buffer.toString('utf8')));
return content;
}

export async function readTextFileLines(filename: string): Promise<string[]> {
const content = await readTextFile(filename);
return content.split('\n');
}

function assertIsBuffer(value: unknown): asserts value is Buffer {
assert(Buffer.isBuffer(value));
}
6 changes: 2 additions & 4 deletions packages/cspell-tools/src/compiler/writeTextToFile.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { promises as fs } from 'fs';
import { promisify } from 'util';
import * as zlib from 'zlib';

const gzip = promisify(zlib.gzip);
import { compress } from '../gzip/index.js';

const isGzFile = /\.gz$/;

export async function writeTextToFile(filename: string, data: string): Promise<void> {
const useGz = isGzFile.test(filename);
const buf = Buffer.from(data, 'utf-8');
const buffer = useGz ? await gzip(buf) : buf;
const buffer = useGz ? await compress(buf) : buf;
await fs.writeFile(filename, buffer);
}

Expand Down
9 changes: 5 additions & 4 deletions packages/cspell-tools/src/gzip/compressFiles.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { readFile, writeFile } from 'node:fs/promises';
import { gzipSync } from 'node:zlib';

import { describe, expect, test, vi } from 'vitest';

import { compress, compressFile, decompress, OSFlags } from './compressFiles.js';
import { compress, compressFile, compressSync, decompress, OSFlags } from './compressFiles.js';

const content = `
Have a nice day.
Expand All @@ -21,15 +20,17 @@ describe('compressFiles', () => {
test('compressFile', async () => {
await compressFile('README.md');
expect(mockReadFile).toHaveBeenLastCalledWith('README.md');
expect(mockWriteFile).toHaveBeenLastCalledWith('README.md.gz', gzipSync(content));
expect(mockWriteFile).toHaveBeenLastCalledWith('README.md.gz', compressSync(content));
});

test('compress/decompress string', async () => {
const gzBufA = await compress(content);
const gzBufA = await compress(content, OSFlags.auto);
const gzBufB = await compress(content, OSFlags.NTFS);
const gzBufC = await compress(content, OSFlags.Unix);
const gzBufDef = await compress(content);

expect(gzBufB).not.toEqual(gzBufC);
expect(gzBufDef).toEqual(gzBufC);

const strA = await decompress(gzBufA, 'utf8');
const strB = await decompress(gzBufB, 'utf8');
Expand Down
15 changes: 12 additions & 3 deletions packages/cspell-tools/src/gzip/compressFiles.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { readFile, writeFile } from 'node:fs/promises';
import { promisify } from 'node:util';
import { gunzip as gunzipCB, gzip as gz } from 'node:zlib';
import { gunzip as gunzipCB, gzip as gz, gzipSync } from 'node:zlib';

const gzip = promisify(gz);
const gunzip = promisify(gunzipCB);

export enum OSFlags {
auto = -1,
FAT = 0,
Unix = 3,
HPFS = 6, // cspell:ignore hpfs
Expand All @@ -28,11 +29,19 @@ export async function compressFile(file: string, os?: OSFlags): Promise<string>
}

export async function compress(buf: string | Uint8Array | Buffer, os?: OSFlags): Promise<Uint8Array> {
const zBuf = await gzip(buf);
const osFlag = os ?? zBuf[OSSystemIDOffset];
return fixOSSystemID(await gzip(buf), os);
}

export function compressSync(buf: string | Uint8Array | Buffer, os?: OSFlags): Uint8Array {
return fixOSSystemID(gzipSync(buf), os);
}

function fixOSSystemID(zBuf: Uint8Array, os: OSFlags = OSFlags.Unix): Uint8Array {
const osFlag = os == OSFlags.auto ? zBuf[OSSystemIDOffset] : os;
zBuf[OSSystemIDOffset] = osFlag;
return zBuf;
}

export async function decompress(buf: Uint8Array | Buffer, encoding?: undefined): Promise<Uint8Array>;
export async function decompress(buf: Uint8Array | Buffer, encoding: 'utf8'): Promise<string>;
export async function decompress(buf: Uint8Array | Buffer, encoding: 'utf8' | undefined): Promise<string | Uint8Array>;
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-tools/src/gzip/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { compressFile, OSFlags } from './compressFiles.js';
export { compress, compressFile, decompress, OSFlags } from './compressFiles.js';
export { gzip } from './gzip.js';

0 comments on commit bdb384b

Please sign in to comment.