2020-08-19 13:27:35 -05:00
|
|
|
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) => {
|
2020-09-16 23:48:16 -05:00
|
|
|
expect(error.message).toMatch(
|
|
|
|
/ENOENT: no such file or directory, stat '(.*)package.fail.json'/
|
|
|
|
);
|
2020-08-19 13:27:35 -05:00
|
|
|
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();
|
2020-08-31 15:21:38 -05:00
|
|
|
expect(data).toMatchInlineSnapshot(`
|
|
|
|
"{
|
|
|
|
\\"name\\": \\"assets\\",
|
|
|
|
\\"version\\": \\"0.0.1\\"
|
|
|
|
}
|
|
|
|
"
|
|
|
|
`);
|
2020-08-19 13:27:35 -05:00
|
|
|
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();
|
2020-08-31 15:21:38 -05:00
|
|
|
expect(data).toMatchInlineSnapshot(`
|
|
|
|
Object {
|
|
|
|
"name": "assets",
|
|
|
|
"version": "0.0.1",
|
|
|
|
}
|
|
|
|
`);
|
2020-08-19 13:27:35 -05:00
|
|
|
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) => {
|
2020-09-16 23:48:16 -05:00
|
|
|
expect(error.message).toMatch(
|
|
|
|
/ENOENT: no such file or directory, open '(.*)package.fail.json'/
|
|
|
|
);
|
2020-08-19 13:27:35 -05:00
|
|
|
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) => {
|
2020-08-31 15:21:38 -05:00
|
|
|
expect(error.message).toMatch(/Unexpected token } in JSON at position \d+/);
|
2020-08-19 13:27:35 -05:00
|
|
|
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();
|
2020-08-31 15:21:38 -05:00
|
|
|
expect(data).toMatchInlineSnapshot(`
|
|
|
|
Object {
|
|
|
|
"name": "assets",
|
|
|
|
"version": "0.0.1",
|
|
|
|
}
|
|
|
|
`);
|
2020-08-19 13:27:35 -05:00
|
|
|
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) => {
|
2020-08-31 15:21:38 -05:00
|
|
|
expect(error.message).toMatch(/Unexpected token } in JSON at position \d+/);
|
2020-08-19 13:27:35 -05:00
|
|
|
removeTempFile('wrong.package.json.lock');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|