Skip to content

Commit ecbc201

Browse files
committed
Change the git root functions to be constants
1 parent 3643979 commit ecbc201

File tree

12 files changed

+30
-58
lines changed

12 files changed

+30
-58
lines changed

lib/buildtools/src/commands/__tests__/lint.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as utils from '@sourceacademy/modules-repotools/getGitRoot';
21
import * as manifest from '@sourceacademy/modules-repotools/manifest';
32
import { ESLint } from 'eslint';
43
import { beforeEach, describe, expect, test, vi } from 'vitest';
@@ -7,7 +6,10 @@ import { getCommandRunner } from './testingUtils.js';
76

87
const lintFilesMock = vi.hoisted(() => vi.fn());
98

10-
vi.spyOn(utils, 'getGitRoot').mockResolvedValue('/');
9+
vi.mock(import('@sourceacademy/modules-repotools/getGitRoot'), async importOriginal => ({
10+
...await importOriginal(),
11+
gitRoot: '/'
12+
}));
1113

1214
const mockedResolveEither = vi.spyOn(manifest, 'resolveEitherBundleOrTab');
1315

lib/buildtools/src/commands/__tests__/template.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import fs from 'fs/promises';
2-
3-
import * as gitRoot from '@sourceacademy/modules-repotools/getGitRoot';
42
import * as manifest from '@sourceacademy/modules-repotools/manifest';
53
import { beforeEach, describe, expect, it, test, vi } from 'vitest';
64
import { askQuestion } from '../../templates/print.js';
75
import getTemplateCommand from '../template.js';
86
import { getCommandRunner } from './testingUtils.js';
97

10-
vi.spyOn(gitRoot, 'getBundlesDir').mockResolvedValue('/src/bundles');
11-
vi.spyOn(gitRoot, 'getTabsDir').mockResolvedValue('/src/tabs');
8+
vi.mock(import('@sourceacademy/modules-repotools/getGitRoot'), async importOriginal => ({
9+
...await importOriginal(),
10+
bundlesDir: '/src/bundles',
11+
tabsDir: '/src/tabs',
12+
}));
1213

1314
vi.mock(import('../../templates/print.js'), async importActual => {
1415
const actualTemplates = await importActual();

lib/buildtools/src/commands/build.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Command } from '@commander-js/extra-typings';
2-
import { getBundlesDir, getOutDir } from '@sourceacademy/modules-repotools/getGitRoot';
2+
import { bundlesDir, outDir } from '@sourceacademy/modules-repotools/getGitRoot';
33
import { resolveAllBundles, resolveEitherBundleOrTab, resolveSingleBundle, resolveSingleTab } from '@sourceacademy/modules-repotools/manifest';
44
import { buildAll } from '../build/all.js';
55
import { buildHtml, buildSingleBundleDocs } from '../build/docs/index.js';
@@ -9,9 +9,6 @@ import { buildManifest } from '../build/modules/manifest.js';
99
import { runBuilderWithPrebuild } from '../prebuild/index.js';
1010
import { lintOption, logCommandErrorAndExit, logLevelOption, processResult, tscOption } from './commandUtils.js';
1111

12-
const outDir = await getOutDir();
13-
const bundlesDir = await getBundlesDir();
14-
1512
// Commands that are specific to a tab or bundle
1613

1714
export const getBuildBundleCommand = () => new Command('bundle')

lib/buildtools/src/commands/list.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Command } from '@commander-js/extra-typings';
2-
import { getBundlesDir, getTabsDir } from '@sourceacademy/modules-repotools/getGitRoot';
2+
import { bundlesDir, tabsDir } from '@sourceacademy/modules-repotools/getGitRoot';
33
import { resolveAllBundles, resolveAllTabs, resolveSingleBundle, resolveSingleTab } from '@sourceacademy/modules-repotools/manifest';
44
import chalk from 'chalk';
55
import omit from 'lodash/omit.js';
@@ -10,7 +10,6 @@ export const getListBundlesCommand = () => new Command('bundle')
1010
.argument('[directory]')
1111
.action(async directory => {
1212
if (directory === undefined) {
13-
const bundlesDir = await getBundlesDir();
1413
const manifestResult = await resolveAllBundles(bundlesDir);
1514
if (manifestResult.severity === 'error') {
1615
logCommandErrorAndExit(manifestResult);
@@ -43,11 +42,6 @@ export const getListTabsCommand = () => new Command('tabs')
4342
.argument('[directory]')
4443
.action(async directory => {
4544
if (directory === undefined) {
46-
const [bundlesDir, tabsDir] = await Promise.all([
47-
getBundlesDir(),
48-
getTabsDir()
49-
]);
50-
5145
const tabsManifest = await resolveAllTabs(bundlesDir, tabsDir);
5246
if (tabsManifest.severity === 'error') {
5347
logCommandErrorAndExit(tabsManifest);

lib/buildtools/src/commands/template.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Interface } from 'readline/promises';
22
import { Command } from '@commander-js/extra-typings';
3-
import { getBundlesDir, getTabsDir } from '@sourceacademy/modules-repotools/getGitRoot';
4-
3+
import { bundlesDir, tabsDir } from '@sourceacademy/modules-repotools/getGitRoot';
54
import { isNodeError } from '@sourceacademy/modules-repotools/utils';
65
import { addNew as addNewModule } from '../templates/bundle.js';
76
import { askQuestion, error as _error, getRl, info, warn } from '../templates/print.js';
@@ -22,11 +21,6 @@ export default function getTemplateCommand() {
2221
return new Command('template')
2322
.description('Interactively create a new module or tab')
2423
.action(async () => {
25-
const [bundlesDir, tabsDir] = await Promise.all([
26-
getBundlesDir(),
27-
getTabsDir()
28-
]);
29-
3024
const rl = getRl();
3125
try {
3226
const mode = await askMode(rl);

lib/buildtools/src/commands/testing.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pathlib from 'path';
22
import { Command, Option } from '@commander-js/extra-typings';
3-
import { getGitRoot } from '@sourceacademy/modules-repotools/getGitRoot';
3+
import { gitRoot } from '@sourceacademy/modules-repotools/getGitRoot';
44
import chalk from 'chalk';
55
import type { VitestRunMode } from 'vitest/node';
66
import { runVitest } from '../testing/runner.js';
@@ -25,7 +25,6 @@ export const getTestCommand = () => new Command('test')
2525
.option('-p, --project <directory>', 'Path to the directory that is the root of your test project')
2626
.argument('[patterns...]', 'Test patterns to filter by.')
2727
.action(async (patterns, { mode, project, ...options }) => {
28-
const gitRoot = await getGitRoot();
2928
const relative = pathlib.relative(project ?? process.cwd(), gitRoot);
3029

3130
if (relative && !relative.startsWith('..') && !pathlib.isAbsolute(relative)) {

lib/buildtools/src/prebuild/lint.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { promises as fs, type Dirent } from 'fs';
22
import pathlib from 'path';
3-
import { getBundlesDir, getGitRoot, getOutDir, getTabsDir } from '@sourceacademy/modules-repotools/getGitRoot';
3+
import { bundlesDir, gitRoot, outDir, tabsDir } from '@sourceacademy/modules-repotools/getGitRoot';
44
import type { InputAsset, Severity } from '@sourceacademy/modules-repotools/types';
55
import { findSeverity, flatMapAsync, isNodeError } from '@sourceacademy/modules-repotools/utils';
66
import chalk from 'chalk';
@@ -35,7 +35,6 @@ async function timePromise<T>(f: () => Promise<T>) {
3535
}
3636

3737
export async function runEslint(input: InputAsset, fix: boolean, stats: boolean): Promise<LintResult> {
38-
const gitRoot = await getGitRoot();
3938
const linter = new ESLint({
4039
fix,
4140
stats,
@@ -49,7 +48,6 @@ export async function runEslint(input: InputAsset, fix: boolean, stats: boolean)
4948
}
5049

5150
if (stats) {
52-
const outDir = await getOutDir();
5351
await fs.mkdir(`${outDir}/lintstats`, { recursive: true });
5452

5553
const statsFormatter = await linter.loadFormatter('json');
@@ -104,12 +102,6 @@ interface LintGlobalResults {
104102
* so that the bundles and tabs can be linted separately.
105103
*/
106104
export async function lintGlobal(fix: boolean): Promise<LintGlobalResults> {
107-
const [gitRoot, bundlesDir, tabsDir] = await Promise.all([
108-
getGitRoot(),
109-
getBundlesDir(),
110-
getTabsDir()
111-
]);
112-
113105
const linter = new ESLint({ fix, cwd: gitRoot });
114106

115107
/**

lib/buildtools/src/testing/utils.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs/promises';
22
import pathlib from 'path';
3-
import { getGitRoot } from '@sourceacademy/modules-repotools/getGitRoot';
3+
import { gitRoot } from '@sourceacademy/modules-repotools/getGitRoot';
44
import { resolveSingleBundle, resolveSingleTab } from '@sourceacademy/modules-repotools/manifest';
55
import type { ErrorResult } from '@sourceacademy/modules-repotools/types';
66
import { isNodeError, mapAsync } from '@sourceacademy/modules-repotools/utils';
@@ -13,8 +13,6 @@ import { loadVitestConfigFromDir, sharedTabsConfig, sharedVitestConfiguration }
1313
// Assign to each configuration a different colour :)
1414
const colors: Readonly<LabelColor[]> = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'];
1515

16-
const gitRoot = await getGitRoot();
17-
1816
/**
1917
* If the vitest project has browser mode enabled, then remove the regular test project as that will run duplicate tests
2018
* Also, unless `watch` is true, the tests should be run in headless mode.
@@ -214,7 +212,6 @@ export async function getTestConfiguration(directory: string, watch: boolean): P
214212
* Based on the root Vitest's configuration, get all the projects we need to test
215213
*/
216214
export async function getAllTestConfigurations(watch: boolean) {
217-
const gitRoot = await getGitRoot();
218215
const rootConfig = await loadVitestConfigFromDir(gitRoot);
219216
if (!rootConfig?.test?.projects) return [];
220217

lib/buildtools/vitest.setup.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ vi.mock(import('typescript'), async importOriginal => {
5353
};
5454
});
5555

56-
vi.mock(import('@sourceacademy/modules-repotools/getGitRoot'), async () => {
56+
vi.mock(import('@sourceacademy/modules-repotools/getGitRoot'), () => {
5757
const testMocksDir = pathlib.resolve(import.meta.dirname, '../__test_mocks__');
5858
return {
59-
getGitRoot: () => Promise.resolve(testMocksDir),
60-
getBundlesDir: () => Promise.resolve(pathlib.join(testMocksDir, 'bundles')),
61-
getTabsDir: () => Promise.resolve(pathlib.join(testMocksDir, 'tabs')),
62-
getOutDir: () => Promise.resolve('/build'),
59+
gitRoot: testMocksDir,
60+
bundlesDir: pathlib.join(testMocksDir, 'bundles'),
61+
tabsDir: pathlib.join(testMocksDir, 'tabs'),
62+
outDir: '/build',
6363
};
6464
});
6565

lib/repotools/src/getGitRoot.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { execFile } from 'child_process';
22
import pathlib from 'path';
3-
import memoize from 'lodash/memoize.js';
43

54
/*
65
* Due to the multi-level directory structure in the repository, some commands can be run anywhere
@@ -21,21 +20,21 @@ function rawGetGitRoot() {
2120
}
2221

2322
/**
24-
* Get the path to the root of the git repository
23+
* Path to the root of the git repository
2524
*/
26-
export const getGitRoot = memoize(rawGetGitRoot);
25+
export const gitRoot = await rawGetGitRoot();
2726

2827
/**
29-
* Get the path to the directory within which bundles are stored
28+
* Path to the directory within which bundles are defined
3029
*/
31-
export const getBundlesDir = memoize(async () => pathlib.join(await getGitRoot(), 'src', 'bundles'));
30+
export const bundlesDir = pathlib.join(gitRoot, 'src', 'bundles');
3231

3332
/**
34-
* Get the path to the directory within which tabs are stored
33+
* Path to the directory within which tabs are defined
3534
*/
36-
export const getTabsDir = memoize(async () => pathlib.join(await getGitRoot(), 'src', 'tabs'));
35+
export const tabsDir = pathlib.join(gitRoot, 'src', 'tabs');
3736

3837
/**
39-
* Get the path to the default output directory
38+
* Path to the default output directory
4039
*/
41-
export const getOutDir = memoize(async () => pathlib.join(await getGitRoot(), 'build'));
40+
export const outDir = pathlib.join(gitRoot, 'build');

0 commit comments

Comments
 (0)