Skip to content

Commit

Permalink
Fix hash when changing directories
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoni committed Jul 28, 2024
1 parent ca7a23b commit 0ae6f25
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
5 changes: 4 additions & 1 deletion bin/eslint_d.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ const command = process.argv[2];
}

// eslint-disable-next-line prefer-const
let [config, hash] = await Promise.all([loadConfig(resolver), filesHash()]);
let [config, hash] = await Promise.all([
loadConfig(resolver),
filesHash(resolver.base)
]);
switch (command) {
case 'start':
if (config) {
Expand Down
7 changes: 4 additions & 3 deletions lib/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ const files = [
];

/**
* @param {string} base
* @returns {Promise<string>}
*/
export async function filesHash() {
const cwd = process.cwd();
export async function filesHash(base) {
const project = path.resolve(base, '../..');
const results = await Promise.allSettled(
files.map((file) => fs.readFile(path.join(cwd, file)))
files.map((file) => fs.readFile(path.join(project, file)))
);
const hash = crypto.createHash('md5');
for (const result of results) {
Expand Down
20 changes: 11 additions & 9 deletions lib/hash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,34 @@ import { filesHash } from './hash.js';

describe('lib/hash', () => {
context('filesHash', () => {
const base = 'some/dir/node_modules/eslint';
const cwd = process.cwd();

it('reads files from cwd', () => {
sinon.replace(process, 'cwd', sinon.fake.returns('some/dir'));
sinon.replace(fs, 'readFile', sinon.fake.returns(sinon.promise()));

filesHash();
filesHash(base);

assert.callCount(fs.readFile, 5);
assert.calledWith(fs.readFile, 'some/dir/package.json');
assert.calledWith(fs.readFile, 'some/dir/package-lock.json');
assert.calledWith(fs.readFile, 'some/dir/npm-shrinkwrap.json');
assert.calledWith(fs.readFile, 'some/dir/yarn.lock');
assert.calledWith(fs.readFile, 'some/dir/pnpm-lock.yaml');
assert.calledWith(fs.readFile, `${cwd}/some/dir/package.json`);
assert.calledWith(fs.readFile, `${cwd}/some/dir/package-lock.json`);
assert.calledWith(fs.readFile, `${cwd}/some/dir/npm-shrinkwrap.json`);
assert.calledWith(fs.readFile, `${cwd}/some/dir/yarn.lock`);
assert.calledWith(fs.readFile, `${cwd}/some/dir/pnpm-lock.yaml`);
});

it('resolves with hash of all files', async () => {
sinon.replace(fs, 'readFile', sinon.fake.resolves('file content'));

const promise = filesHash();
const promise = filesHash(base);

await assert.resolves(promise, 'Jd1/4sH1IanXsNIo1eM+Jw==');
});

it('resolves with null hash if none of the files can be resolved', async () => {
sinon.replace(fs, 'readFile', sinon.fake.rejects());

const promise = filesHash();
const promise = filesHash(base);

await assert.resolves(promise, '1B2M2Y8AsgTpgAmY7PhCfg==');
});
Expand Down

0 comments on commit 0ae6f25

Please sign in to comment.