Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: test
on:
pull_request:
branches: [feature/development-v2]
branches: [v2-dev, v2-staging, v2-beta]
push:
branches: [feature/development-v2]
branches: [v2-dev, v2-staging, v2-beta]

jobs:
test:
Expand All @@ -16,4 +16,4 @@ jobs:
- run: npm ci
- run: npm run lint
- run: npm run build
- run: npm run test:coverage
- run: npm test
18 changes: 17 additions & 1 deletion src/functions/os-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs';
import fs, { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';

Expand Down Expand Up @@ -54,6 +54,22 @@ describe('walkFileSystem', () => {
expect(await collect(workspace)).toEqual([]);
});

it('yields the entries of a directory in name order whatever order the file system returns them in', async () => {
const fileEntry = (name: string) => ({ name, isDirectory: () => false, isFile: () => true });
async function* entriesInDiskOrder() {
yield fileEntry('b.js');
yield fileEntry('a.js');
}
jest.spyOn(fs.promises, 'opendir').mockResolvedValue(entriesInDiskOrder() as unknown as fs.Dir);

const found: string[] = [];
for await (const filePath of walkFileSystem(workspace)) {
found.push(filePath);
}

expect(found).toEqual([join(workspace, 'a.js'), join(workspace, 'b.js')]);
});

it('rejects when the directory does not exist', async () => {
await expect(collect(join(workspace, 'absent'))).rejects.toMatchObject({ code: 'ENOENT' });
});
Expand Down
8 changes: 6 additions & 2 deletions src/functions/os-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import fs from 'fs';
import { join, normalize } from 'path';

export async function* walkFileSystem(directory: string): AsyncGenerator<string> {
const fileSystemIterator = await fs.promises.opendir(directory);
const fileSystemElements: fs.Dirent[] = [];
for await (const fileSystemElement of await fs.promises.opendir(directory)) {
fileSystemElements.push(fileSystemElement);
}
fileSystemElements.sort((left, right) => Number(left.name > right.name) - Number(left.name < right.name));

for await (const fileSystemElement of fileSystemIterator) {
for (const fileSystemElement of fileSystemElements) {
const filePath = normalize(join(directory, fileSystemElement.name));

if (fileSystemElement.isDirectory()) {
Expand Down
Loading