Skip to content

Commit

Permalink
fix(windows): normalise file paths
Browse files Browse the repository at this point in the history
Refs #66
  • Loading branch information
JamieMason committed May 29, 2022
1 parent 2d9516f commit c5e87c2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
24 changes: 12 additions & 12 deletions src/bin-format/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ describe('format', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).toHaveBeenCalledWith(
expect.stringMatching('/some/package.json'),
expect.stringContaining('/some/package.json'),
json,
);
expect(before).toEqual(after);
expect(log).toHaveBeenCalledWith(
expect.stringMatching(//),
expect.stringMatching('/some/package.json'),
expect.stringContaining('/some/package.json'),
);
});
it('sorts object properties alphabetically by key', () => {
Expand All @@ -41,13 +41,13 @@ describe('format', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).toHaveBeenCalledWith(
expect.stringMatching('/some/package.json'),
expect.stringContaining('/some/package.json'),
json,
);
expect(before).toEqual(after);
expect(log).toHaveBeenCalledWith(
expect.stringMatching(//),
expect.stringMatching('/some/package.json'),
expect.stringContaining('/some/package.json'),
);
});
it('sorts named properties first, then the rest alphabetically', () => {
Expand All @@ -63,13 +63,13 @@ describe('format', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).toHaveBeenCalledWith(
expect.stringMatching('/some/package.json'),
expect.stringContaining('/some/package.json'),
json,
);
expect(before).toEqual(after);
expect(log).toHaveBeenCalledWith(
expect.stringMatching(//),
expect.stringMatching('/some/package.json'),
expect.stringContaining('/some/package.json'),
);
});
it('uses shorthand format for "bugs"', () => {
Expand All @@ -84,13 +84,13 @@ describe('format', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).toHaveBeenCalledWith(
expect.stringMatching('/some/package.json'),
expect.stringContaining('/some/package.json'),
json,
);
expect(before).toEqual(after);
expect(log).toHaveBeenCalledWith(
expect.stringMatching(//),
expect.stringMatching('/some/package.json'),
expect.stringContaining('/some/package.json'),
);
});
it('uses shorthand format for "repository"', () => {
Expand All @@ -107,13 +107,13 @@ describe('format', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).toHaveBeenCalledWith(
expect.stringMatching('/some/package.json'),
expect.stringContaining('/some/package.json'),
json,
);
expect(before).toEqual(after);
expect(log).toHaveBeenCalledWith(
expect.stringMatching(//),
expect.stringMatching('/some/package.json'),
expect.stringContaining('/some/package.json'),
);
});
it('uses github shorthand format for "repository"', () => {
Expand All @@ -130,13 +130,13 @@ describe('format', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).toHaveBeenCalledWith(
expect.stringMatching('/some/package.json'),
expect.stringContaining('/some/package.json'),
json,
);
expect(before).toEqual(after);
expect(log).toHaveBeenCalledWith(
expect.stringMatching(//),
expect.stringMatching('/some/package.json'),
expect.stringContaining('/some/package.json'),
);
});
});
35 changes: 18 additions & 17 deletions src/lib/get-input/get-input.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'expect-more-jest';
import { join } from 'path';
import { getInput } from '.';
import { mockDisk } from '../../../test/mock-disk';
import { DEFAULT_CONFIG } from '../../constants';
Expand Down Expand Up @@ -133,13 +134,13 @@ describe('getInput', () => {
describe('for a single package.json file', () => {
it('reads that file only', () => {
const CWD = process.cwd();
const filePath = `${CWD}/package.json`;
const filePath = join(CWD, 'package.json');
const contents = { name: 'foo' };
const json = '{"name":"foo"}';
const disk = mockDisk();
disk.globSync.mockReturnValue([filePath]);
disk.readFileSync.mockReturnValue(json);
expect(getInput(disk, { source: ['package.json'] })).toEqual(
expect(getInput(disk, { source: ['./package.json'] })).toEqual(
expect.objectContaining({
wrappers: [{ filePath, contents, json }],
}),
Expand Down Expand Up @@ -171,32 +172,32 @@ describe('getInput', () => {
describe('as an array', () => {
it('resolves yarn workspace packages', () => {
const CWD = process.cwd();
const filePath = `${CWD}/package.json`;
const contents = { workspaces: ['as-array/*'] };
const filePath = join(CWD, 'package.json');
const contents = { workspaces: ['./as-array/*'] };
const json = JSON.stringify(contents);
const disk = mockDisk();
disk.globSync.mockReturnValue([filePath]);
disk.readFileSync.mockReturnValue(json);
getInput(disk, {});
expect(disk.globSync.mock.calls).toEqual([
['package.json'],
['as-array/*/package.json'],
['./package.json'],
['./as-array/*/package.json'],
]);
});
});
describe('as an object', () => {
it('resolves yarn workspace packages', () => {
const CWD = process.cwd();
const filePath = `${CWD}/package.json`;
const contents = { workspaces: { packages: ['as-object/*'] } };
const filePath = join(CWD, 'package.json');
const contents = { workspaces: { packages: ['./as-object/*'] } };
const json = JSON.stringify(contents);
const disk = mockDisk();
disk.globSync.mockReturnValue([filePath]);
disk.readFileSync.mockReturnValue(json);
getInput(disk, {});
expect(disk.globSync.mock.calls).toEqual([
['package.json'],
['as-object/*/package.json'],
['./package.json'],
['./as-object/*/package.json'],
]);
});
});
Expand All @@ -205,37 +206,37 @@ describe('getInput', () => {
describe('when lerna.json is defined', () => {
it('resolves lerna packages', () => {
const CWD = process.cwd();
const filePath = `${CWD}/package.json`;
const filePath = join(CWD, 'package.json');
const contents = { name: 'foo' };
const json = JSON.stringify(contents);
const disk = mockDisk();
disk.globSync.mockReturnValue([filePath]);
disk.readFileSync.mockImplementation((filePath) => {
if (filePath.endsWith('package.json')) return json;
if (filePath.endsWith('lerna.json'))
return JSON.stringify({ packages: ['lerna/*'] });
return JSON.stringify({ packages: ['./lerna/*'] });
});
getInput(disk, {});
expect(disk.globSync.mock.calls).toEqual([
['package.json'],
['lerna/*/package.json'],
['./package.json'],
['./lerna/*/package.json'],
]);
});
});
describe('when lerna.json is not defined', () => {
describe('when pnpm workspaces are defined', () => {
it('resolves pnpm packages', () => {
const CWD = process.cwd();
const filePath = `${CWD}/package.json`;
const filePath = join(CWD, 'package.json');
const disk = mockDisk();
disk.globSync.mockReturnValue([filePath]);
disk.readYamlFileSync.mockReturnValue({
packages: ['./from-pnpm/*'],
});
getInput(disk, {});
expect(disk.globSync.mock.calls).toEqual([
['package.json'],
['from-pnpm/*/package.json'],
['./package.json'],
['./from-pnpm/*/package.json'],
]);
});
});
Expand Down
5 changes: 3 additions & 2 deletions src/lib/get-input/get-wrappers/get-patterns/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { isArrayOfStrings } from 'expect-more';
import { flow, pipe } from 'fp-ts/lib/function';
import * as O from 'fp-ts/lib/Option';
import { join } from 'path';
import type { SyncpackConfig } from '../../../../constants';
import { ALL_PATTERNS } from '../../../../constants';
import type { Disk } from '../../../../lib/disk';
Expand Down Expand Up @@ -46,6 +45,8 @@ export function getPatterns(disk: Disk, program: SyncpackConfig): Patterns {
}

function limitToPackageJson(patterns: Patterns): Patterns {
return patterns.map((pattern) => join(pattern, 'package.json'));
return patterns.map((pattern) =>
pattern.includes('package.json') ? pattern : `${pattern}/package.json`,
);
}
}
10 changes: 5 additions & 5 deletions test/scenarios/create-scenario.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import minimatch from 'minimatch';
import path from 'path';
import { join } from 'path';
import type { SyncpackConfig } from '../../src/constants';
import type { SourceWrapper } from '../../src/lib/get-input/get-wrappers';
import type { MockDisk } from '../mock-disk';
Expand Down Expand Up @@ -34,17 +34,17 @@ export function createScenario(
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
// resolve all paths
const mockedFiles: MockedFile[] = fileMocks.map((file) => ({
absolutePath: path.join(process.cwd(), file.path),
absolutePath: join(process.cwd(), file.path),
after: file.after,
before: file.before,
diskWriteWhenChanged: [expect.stringContaining(file.path), file.after.json],
logEntryWhenChanged: [
expect.stringMatching(//),
expect.stringMatching(file.path),
expect.stringContaining(file.path),
],
logEntryWhenUnchanged: [
expect.stringMatching(/-/),
expect.stringMatching(file.path),
expect.stringContaining(file.path),
],
relativePath: file.path,
}));
Expand All @@ -58,7 +58,7 @@ export function createScenario(
disk.globSync.mockImplementation((pattern): string[] => {
return mockedFiles
.filter((file) => {
return minimatch(file.absolutePath, path.join(process.cwd(), pattern));
return minimatch(file.absolutePath, join(process.cwd(), pattern));
})
.map((file) => file.absolutePath);
});
Expand Down

0 comments on commit c5e87c2

Please sign in to comment.