Skip to content

Commit

Permalink
test: 🐛 resolve bad recursive delete dirs and usage of rimraf
Browse files Browse the repository at this point in the history
  • Loading branch information
luffynando committed Sep 17, 2024
1 parent 95f6106 commit 12fad4e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 31 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"jest-xml-matcher": "^1.2.0",
"np": "^10.0.7",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"tshy": "^3.0.2",
"typedoc": "^0.26.7",
"typescript": "^5.6.2",
Expand Down
22 changes: 17 additions & 5 deletions tests/test_utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { existsSync, readFileSync, rmSync, statSync } from 'node:fs';
import { existsSync, readFileSync, statSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { rimraf } from 'rimraf';

/**
* Get filename for a given file path URL
Expand Down Expand Up @@ -36,7 +37,8 @@ export const fileContent = (file: string): string => {
export const fileContents = (append: string): string => fileContent(filePath(append));

export const deleteDirectory = async (dirname: string): Promise<void> =>
new Promise<void>((resolve) => {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
new Promise<void>((resolve, reject) => {
if (!existsSync(dirname)) {
resolve();

Expand All @@ -50,7 +52,17 @@ export const deleteDirectory = async (dirname: string): Promise<void> =>
return;
}

rmSync(dirname, { recursive: true, force: true });

resolve();
// eslint-disable-next-line no-promise-executor-return
return (
rimraf(dirname)
// eslint-disable-next-line promise/always-return, promise/prefer-await-to-then
.then(() => {
resolve();
})
// eslint-disable-next-line promise/prefer-await-to-then
.catch((error: unknown) => {
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
reject(error);
})
);
});
26 changes: 11 additions & 15 deletions tests/unit/common_xml_retriever.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import path from 'node:path';
import { globSync } from 'glob';
import AbstractBaseRetriever from '#src/abstract_base_retriever';
import NodeDownloader from '#src/downloader/node_downloader';
import { buildPath, deleteDirectory, fileContent, getDirname, publicPath } from '../test_utils.js';
import { buildPath, fileContent, getDirname, publicPath } from '../test_utils.js';
import useRetriever from '../use_retriever.js';
import CommonXmlRetriever from './common_xml_retriever.js';

describe('common xml retriever', () => {
const { pathToClear } = useRetriever();

test('construct minimal', () => {
const retriever = new CommonXmlRetriever('foo');

Expand Down Expand Up @@ -41,6 +44,7 @@ describe('common xml retriever', () => {

test('download_simple_case', async () => {
const localPath = buildPath('foo');
pathToClear(localPath);
const remoteFile = 'http://localhost:8999/xsd/simple.xsd';
const publicFile = publicPath('xsd/simple.xsd');

Expand All @@ -61,24 +65,22 @@ describe('common xml retriever', () => {
const downloadedXml = fileContent(downloaded);

expect(downloadedXml).toEqualXML(publicXml);

await deleteDirectory(localPath);
}, 30_000);

test('download_throws_exception_on_empty_file', async () => {
const localPath = buildPath('empty');
pathToClear(localPath);
const remote = 'http://localhost:8999/other/empty.xml';
const retriever = new CommonXmlRetriever(localPath);
const t = async (): Promise<string> => retriever.download(remote);

await expect(t).rejects.toBeInstanceOf(Error);
await expect(t).rejects.toThrow(`The source ${remote} is not an xml file because it is empty`);

await deleteDirectory(localPath);
}, 30_000);

test('download_not_an_xml_file_throws_an_exception_and_remove_the_file', async () => {
const localPath = buildPath('other');
pathToClear(localPath);
const remote = 'http://localhost:8999/other/sample.gz';
const retriever = new CommonXmlRetriever(localPath);

Expand All @@ -90,12 +92,11 @@ describe('common xml retriever', () => {
// Assert that the file does not exist (even if it was downloaded)
const local = retriever.buildPath(remote);
expect(existsSync(local)).toBeFalsy();

await deleteDirectory(localPath);
}, 30_000);

test('download_non_existent', async () => {
const localPath = buildPath('non-existent');
pathToClear(localPath);
const remote = 'http://localhost:8999/non-existent-resource.txt';
const retriever = new CommonXmlRetriever(localPath);

Expand All @@ -105,8 +106,6 @@ describe('common xml retriever', () => {

await expect(t).rejects.toBeInstanceOf(Error);
await expect(t).rejects.toThrow(`Unable to download ${remote} to ${destination}`);

await deleteDirectory(localPath);
}, 30_000);

test('download_to_non_writable', async () => {
Expand All @@ -127,12 +126,11 @@ describe('common xml retriever', () => {
'retrieve_xml_valid_cases %s',
async (_name: string, remote: string) => {
const localPath = buildPath('sample');
pathToClear(localPath);
const retriever = new CommonXmlRetriever(localPath);
const response = await retriever.retrieve(remote);

expect(response).not.toBe('');

await deleteDirectory(localPath);
},
30_000,
);
Expand All @@ -144,6 +142,7 @@ describe('common xml retriever', () => {
'retrieve_xml_with_errors %s',
async (_name: string, remote: string) => {
const localPath = buildPath('malformed');
pathToClear(localPath);
const retriever = new CommonXmlRetriever(localPath);

const t = async (): Promise<string> => retriever.retrieve(remote);
Expand All @@ -153,8 +152,6 @@ describe('common xml retriever', () => {

const local = retriever.buildPath(remote);
expect(existsSync(local)).toBeFalsy();

await deleteDirectory(localPath);
},
30_000,
);
Expand All @@ -173,6 +170,7 @@ describe('common xml retriever', () => {

test('retrieve_with_history', async () => {
const localPath = buildPath('common');
pathToClear(localPath);
const remoteParent = 'http://localhost:8999/other/common/parent.xml';
const expectedRetrievedFiles = ['parent.xml', 'child.xml', 'recursive-self.xml', 'foo.xml'];

Expand All @@ -192,7 +190,5 @@ describe('common xml retriever', () => {
expect(Object.values(history)).toContain(retrievedFile);
expect(expectedRetrievedFiles).toContain(path.basename(retrievedFile));
}

await deleteDirectory(localPath);
}, 30_000);
});
11 changes: 6 additions & 5 deletions tests/unit/xsd_retriever.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import 'jest-xml-matcher';
import { existsSync, readFileSync } from 'node:fs';
import { EOL } from 'node:os';
import XsdRetriever from '#src/xsd_retriever';
import { buildPath, deleteDirectory, fileContent, filePath, publicPath } from '../test_utils.js';
import { buildPath, fileContent, filePath, publicPath } from '../test_utils.js';
import useRetriever from '../use_retriever.js';

describe('xsd retriever', () => {
const { pathToClear } = useRetriever();

test('retrieve recursive', async () => {
const localPath = buildPath('recursive');
pathToClear(localPath);
const retriever = new XsdRetriever(localPath);
const remote = 'http://localhost:8999/xsd/entities/ticket.xsd';
const expectedRemotes = [
Expand All @@ -28,15 +32,14 @@ describe('xsd retriever', () => {
const localXml = fileContent(local);

expect(localXml).toEqualXML(assetXml);

await deleteDirectory(localPath);
});

test.runIf(existsSync(publicPath('www.sat.gob.mx')) && existsSync(publicPath('sat-urls.txt')))(
'retrieve complex structure',
async () => {
const pathSatUrls = publicPath('sat-urls.txt');
const localPath = buildPath('SATXSD');
pathToClear(localPath);
const remotePrefix = 'http://localhost:8999/www.sat.gob.mx/sitio_internet/';
const remote = `${remotePrefix}cfd/3/cfdv33.xsd`;
const retriever = new XsdRetriever(localPath);
Expand All @@ -52,8 +55,6 @@ describe('xsd retriever', () => {
for (const expectedRemote of expectedRemotes) {
expect(existsSync(retriever.buildPath(`${remotePrefix}${expectedRemote}`))).toBeTruthy();
}

await deleteDirectory(localPath);
},
30_000,
);
Expand Down
11 changes: 6 additions & 5 deletions tests/unit/xslt_retriever.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import 'jest-xml-matcher';
import { existsSync, readFileSync } from 'node:fs';
import { EOL } from 'node:os';
import XsltRetriever from '#src/xslt_retriever';
import { buildPath, deleteDirectory, fileContent, filePath, publicPath } from '../test_utils.js';
import { buildPath, fileContent, filePath, publicPath } from '../test_utils.js';
import useRetriever from '../use_retriever.js';

describe('xslt retriever', () => {
const { pathToClear } = useRetriever();

test('retrieve recursive', async () => {
const localPath = buildPath('recursive');
pathToClear(localPath);
const retriever = new XsltRetriever(localPath);
const remote = 'http://localhost:8999/xslt/entities/ticket.xslt';
const expectedRemotes = [
Expand All @@ -28,15 +32,14 @@ describe('xslt retriever', () => {
const localXml = fileContent(local);

expect(localXml).toEqualXML(assetXml);

await deleteDirectory(localPath);
}, 30_000);

test.runIf(existsSync(publicPath('www.sat.gob.mx')) && existsSync(publicPath('sat-urls.txt')))(
'retrieve complex structure',
async () => {
const pathSatUrls = publicPath('sat-urls.txt');
const localPath = buildPath('SATXSLT');
pathToClear(localPath);
const remotePrefix = 'http://localhost:8999/www.sat.gob.mx/sitio_internet/';
const remote = `${remotePrefix}cfd/3/cadenaoriginal_3_3/cadenaoriginal_3_3.xslt`;
const retriever = new XsltRetriever(localPath);
Expand All @@ -52,8 +55,6 @@ describe('xslt retriever', () => {
for (const expectedRemote of expectedRemotes) {
expect(existsSync(retriever.buildPath(`${remotePrefix}${expectedRemote}`))).toBeTruthy();
}

await deleteDirectory(localPath);
},
30_000,
);
Expand Down
33 changes: 33 additions & 0 deletions tests/use_retriever.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { buildPath, deleteDirectory } from './test_utils.js';

const useRetriever = () => {
let selectedPathToClear = '';

const pathToClear = (path = ''): string => {
if (path === '') {
return selectedPathToClear;
}

if (path.indexOf(buildPath(''))) {
throw new Error('Unable to set a path to clear that is not in the build path');
}

const previousPath = selectedPathToClear;
selectedPathToClear = path;

return previousPath;
};

// eslint-disable-next-line vitest/require-top-level-describe
afterEach(async () => {
if (pathToClear() !== '') {
await deleteDirectory(pathToClear());
}
});

return {
pathToClear,
};
};

export default useRetriever;
1 change: 0 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ export default defineConfig({
include: ['src/**/*.ts'],
},
globalSetup: ['./tests/global_setup.ts'],
poolOptions: { threads: { singleThread: true } },
},
});

0 comments on commit 12fad4e

Please sign in to comment.