0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-23 22:27:34 -05:00
verdaccio/packages/core/file-locking/tests/lock.spec.ts
Juan Picado 8c730c0694 refactor: max-len and printWidth to 100 (#1941)
* refactor: max-len and printWidth to 100

* chore: ci specific pnpm version

* fix: add types peer package

literally get rid of types package
2021-04-09 17:54:20 +02:00

135 lines
3.9 KiB
TypeScript

import path from 'path';
import fs from 'fs';
import { lockFile, unlockFile, readFile } from '../src/index';
interface Error {
message: string;
}
const getFilePath = (filename: string): string => {
return path.resolve(__dirname, `assets/${filename}`);
};
const removeTempFile = (filename: string): void => {
const filepath = getFilePath(filename);
fs.unlink(filepath, (error) => {
if (error) {
throw error;
}
});
};
describe('testing locking', () => {
describe('lockFile', () => {
test('file should be found to be locked', (done) => {
lockFile(getFilePath('package.json'), (error: Error) => {
expect(error).toBeNull();
removeTempFile('package.json.lock');
done();
});
});
test('file should fail to be found to be locked', (done) => {
lockFile(getFilePath('package.fail.json'), (error: Error) => {
expect(error.message).toMatch(
/ENOENT: no such file or directory, stat '(.*)package.fail.json'/
);
done();
});
});
});
describe('unlockFile', () => {
test('file should to be found to be unLock', (done) => {
unlockFile(getFilePath('package.json.lock'), (error: Error) => {
expect(error).toBeNull();
done();
});
});
});
describe('readFile', () => {
test('read file with no options should to be found to be read it as string', (done) => {
readFile(getFilePath('package.json'), {}, (error: Error, data: string) => {
expect(error).toBeNull();
expect(data).toMatchInlineSnapshot(`
"{
\\"name\\": \\"assets\\",
\\"version\\": \\"0.0.1\\"
}
"
`);
done();
});
});
test('read file with no options should to be found to be read it as object', (done) => {
const options = {
parse: true,
};
readFile(getFilePath('package.json'), options, (error: Error, data: string) => {
expect(error).toBeNull();
expect(data).toMatchInlineSnapshot(`
Object {
"name": "assets",
"version": "0.0.1",
}
`);
done();
});
});
test('read file with options (parse) should to be not found to be read it', (done) => {
const options = {
parse: true,
};
readFile(getFilePath('package.fail.json'), options, (error: Error) => {
expect(error.message).toMatch(
/ENOENT: no such file or directory, open '(.*)package.fail.json'/
);
done();
});
});
test('read file with options should to be found to be read it and fails to be parsed', (done) => {
const options = {
parse: true,
};
readFile(getFilePath('wrong.package.json'), options, (error: Error) => {
expect(error.message).toMatch(/Unexpected token } in JSON at position \d+/);
done();
});
});
test('read file with options (parse, lock) should to be found to be read it as object', (done) => {
const options = {
parse: true,
lock: true,
};
readFile(getFilePath('package2.json'), options, (error: Error, data: string) => {
expect(error).toBeNull();
expect(data).toMatchInlineSnapshot(`
Object {
"name": "assets",
"version": "0.0.1",
}
`);
removeTempFile('package2.json.lock');
done();
});
});
test('read file with options (parse, lock) should to be found to be read it and fails to be parsed', (done) => {
const options = {
parse: true,
lock: true,
};
readFile(getFilePath('wrong.package.json'), options, (error: Error) => {
expect(error.message).toMatch(/Unexpected token } in JSON at position \d+/);
removeTempFile('wrong.package.json.lock');
done();
});
});
});
});