Skip to content

Commit a9f2d80

Browse files
committed
Add a "same path" comparer
1 parent 2bcf0dc commit a9f2d80

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

lib/buildtools/src/testing/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { gitRoot } from '@sourceacademy/modules-repotools/getGitRoot';
44
import { resolveSingleBundle, resolveSingleTab } from '@sourceacademy/modules-repotools/manifest';
55
import { baseVitestConfig, loadVitestConfigFromDir, sharedTabsConfig } from '@sourceacademy/modules-repotools/testing';
66
import type { ErrorResult } from '@sourceacademy/modules-repotools/types';
7-
import { isNodeError, mapAsync } from '@sourceacademy/modules-repotools/utils';
7+
import { isNodeError, isSamePath, mapAsync } from '@sourceacademy/modules-repotools/utils';
88
import cloneDeep from 'lodash/cloneDeep.js';
99
import partition from 'lodash/partition.js';
1010
import type { LabelColor } from 'vitest';
@@ -86,7 +86,7 @@ export async function getTestConfiguration(directory: string, watch: boolean): P
8686
* directory, throw an error.
8787
*/
8888
async function findPackageJson(directory: string): Promise<['bundle' | 'tab' | 'config', string] | null> {
89-
if (directory === gitRoot) return null;
89+
if (isSamePath(directory, gitRoot)) return null;
9090

9191
try {
9292
const jsonPath = pathlib.join(directory, 'package.json');

lib/buildtools/vitest.setup.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Buildtools vitest setup
22
import pathlib from 'path';
3+
import { isSamePath } from '@sourceacademy/modules-repotools/utils';
34
import { expect, vi } from 'vitest';
45

56
class MockProcessError extends Error {
@@ -128,10 +129,10 @@ expect.extend({
128129
}
129130
},
130131
toMatchPath(received: string, expected: string) {
131-
const output = pathlib.relative(received, expected);
132+
const result = isSamePath(received, expected);
132133
return {
133-
pass: output === '',
134-
message: () => `${received} ${output === '' ? 'did not resolve' : 'resolved'} to ${expected}`,
134+
pass: result,
135+
message: () => `${received} ${result ? 'did not resolve' : 'resolved'} to ${expected}`,
135136
};
136137
}
137138
});

lib/repotools/src/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,21 @@ type ArrayOfLength<T extends number, U = unknown, V extends U[] = []> =
155155
export function arrayIsLength<T extends number>(obj: unknown, length: T): obj is ArrayOfLength<T> {
156156
return Array.isArray(obj) && obj.length === length;
157157
}
158+
159+
/**
160+
* If the provided path is already a posix path, then this function does nothing.\
161+
* Otherwise, it will return Windows paths with the path separators replaced.
162+
*/
163+
export function convertToPosixPath(p: string) {
164+
return pathlib.posix.join(...p.split(pathlib.sep));
165+
}
166+
167+
/**
168+
* Returns `true` if both paths refer to the same location. This
169+
* function should be OS agnostic
170+
*/
171+
export function isSamePath(lhs: string, rhs: string) {
172+
const relpath = pathlib.relative(lhs, rhs);
173+
console.log('relpath for', lhs, 'and', rhs, 'is', relpath);
174+
return relpath === '';
175+
}

lib/repotools/vitest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const config = {
99
...rootConfig.test,
1010
name: 'Repo Tools',
1111
root: import.meta.dirname,
12-
include: ['**/__tests__/*.test.ts'],
12+
include: ['**/__tests__/**/*.test.ts'],
1313
watch: false,
1414
projects: undefined
1515
}

0 commit comments

Comments
 (0)