Skip to content

Commit

Permalink
Merge pull request #30 from meza/lockfile
Browse files Browse the repository at this point in the history
chore: added lockfile #17
  • Loading branch information
meza authored Jun 25, 2022
2 parents e59a991 + 4b09b7a commit 6af53e4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/lib/adr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export const newAdr = async (title: string, config?: NewOptions) => {
const adrDirectory = await getDir();
const adrPath = path.resolve(path.join(adrDirectory, fileName));
await fs.writeFile(adrPath, adr);
await fs.writeFile(path.resolve(adrDirectory, '.adr-sequence.lock'), newNum.toString());

await processSupersedes(
adrPath,
Expand Down
41 changes: 41 additions & 0 deletions src/lib/numbering.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { afterAll, describe, it, vi, expect } from 'vitest';
import fs from 'fs/promises';
import { newNumber } from './numbering';

type ReaddirMock = () => Promise<String[]>

vi.mock('fs/promises');
vi.mock('./config', () => ({
getDir: vi.fn().mockResolvedValue('/')
}));

describe('The numbering logic', () => {

afterAll(() => {
vi.resetAllMocks();
});
it('can read from the sequence file', async () => {
const random = Math.floor(Math.random() * 100);
vi.mocked(fs.readFile).mockResolvedValueOnce(random.toString());
const num = await newNumber();
expect(num).toEqual(random + 1);
});

it('returns 1 if there are no files', async () => {
vi.mocked(fs.readFile).mockRejectedValueOnce('no sequence file');
vi.mocked(fs.readdir).mockResolvedValueOnce([]);
const num = await newNumber();
expect(num).toEqual(1);
});

it('processes existing files if there is no lockfile', async () => {
const fakeFiles: string[] = [
'0001-first-file.md',
'0002-first-file.md'
];
vi.mocked(fs.readFile).mockRejectedValueOnce('no sequence file');
vi.mocked(fs.readdir as unknown as ReaddirMock).mockResolvedValueOnce(fakeFiles);
const num = await newNumber();
expect(num).toEqual(3);
});
});
31 changes: 20 additions & 11 deletions src/lib/numbering.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import fs from 'fs/promises';
import { getDir } from './config';
import * as path from 'path';

export const newNumber = async () => {
const filePattern = /^0*(\d+)-.*$/;
const files = await fs.readdir(await getDir());
const numbers = files.map(f => {
const adrFile = f.match(filePattern);
if (adrFile) {
return parseInt(adrFile[1], 10);
}
return 0;
});
try {
const lockfile = await fs.readFile(path.resolve(await getDir(), '.adr-sequence.lock'));
return parseInt(lockfile.toString().trim(), 10) + 1;
} catch (e) {
// This is for backward compatibility. If someone upgrades from an older tool without a lockfile,
// we create one.
const filePattern = /^0*(\d+)-.*$/;
const files = await fs.readdir(await getDir());
const numbers = files.map(f => {
const adrFile = f.match(filePattern);
if (adrFile) {
return parseInt(adrFile[1], 10);
}
return 0;
});

const largestNumber = numbers.reduce((a, b) => Math.max(a, b), 0);
return largestNumber + 1;
}

const largestNumber = numbers.reduce((a, b) => Math.max(a, b), 0);
return largestNumber + 1;
};
8 changes: 8 additions & 0 deletions tests/init-adr-repository.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,28 @@ describe('Init an ADR Repository', () => {
it('should use the default directory', () => {
childProcess.execSync(`${command} init`, { cwd: workDir });
const expectedFile: string = path.join(adrDirectory, '0001-record-architecture-decisions.md');
const expectedLockFile: string = path.join(adrDirectory, '.adr-sequence.lock');
expect(fs.existsSync(expectedFile)).toBeTruthy();
expect(fs.existsSync(expectedLockFile)).toBeTruthy();

const fileContents = fs.readFileSync(expectedFile, 'utf8');
const lockFileContents = fs.readFileSync(expectedLockFile, 'utf8');
expect(fileContents).toMatchSnapshot();
expect(lockFileContents).toEqual('1');
});

it('should use an alternate directory', () => {
const directory: string = path.resolve(path.join(workDir, 'tmp', 'alternative-dir'));
childProcess.execSync(`${command} init ${directory}`, { cwd: workDir });

const expectedInitFile: string = path.join(directory, '0001-record-architecture-decisions.md');
const expectedLockFile: string = path.join(directory, '.adr-sequence.lock');
expect(fs.existsSync(expectedInitFile)).toBeTruthy();
expect(fs.existsSync(expectedLockFile)).toBeTruthy();

const fileContents = fs.readFileSync(expectedInitFile, 'utf8');
const lockFileContents = fs.readFileSync(expectedLockFile, 'utf8');
expect(fileContents).toMatchSnapshot();
expect(lockFileContents).toEqual('1');
});
});

0 comments on commit 6af53e4

Please sign in to comment.