Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve fuels init directory detection #3638

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
100 changes: 76 additions & 24 deletions packages/fuels/test/features/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from 'chalk';
import { existsSync, readFileSync } from 'fs';
import { existsSync } from 'fs';

import { Commands } from '../../src';
import { mockCheckForUpdates } from '../utils/mockCheckForUpdates';
Expand All @@ -10,6 +10,7 @@
runInit,
resetDiskAndMocks,
resetConfigAndMocks,
loadFuelsConfig,
} from '../utils/runCommands';

/**
Expand Down Expand Up @@ -39,14 +40,14 @@
});

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();
const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8');
expect(fuelsContents).toMatch(`workspace: './workspace',`);
expect(fuelsContents).toMatch(`output: './output',`);
expect(fuelsContents).not.toMatch(`forcPath: 'fuels-forc',`);
expect(fuelsContents).not.toMatch(`fuelCorePath: 'fuels-core',`);
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
workspace: './workspace',
output: './output',
});
});

it('should run `init` command with --contracts', async () => {
it('should run `init` command with --contracts [absolute path]', async () => {
await runInit({
root: paths.root,
contracts: [paths.contractsBarDir, paths.contractsFooDir],
Expand All @@ -59,10 +60,51 @@
];

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();
const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8');
expect(fuelsContents).toMatch(/contracts:/);
expect(fuelsContents).toMatch(relativeBarDir);
expect(fuelsContents).toMatch(relativeFooDir);
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
contracts: [relativeBarDir, relativeFooDir],
output: './output',
});
});

it('should run `init` command with --contracts [glob path - multiple matches]', async () => {
await runInit({
root: paths.root,
contracts: [`${paths.contractsDir}/*`],
output: paths.outputDir,
});

const relativeContractPaths = [
paths.upgradableChunkedContractPath,
paths.upgradableContractPath,
paths.contractsBarDir,
paths.contractsFooDir,
].map((path) => path.replace(paths.workspaceDir, 'workspace'));

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
contracts: expect.arrayContaining(relativeContractPaths),
output: './output',
});
});

it('should run `init` command with --contracts [glob path - single path]', async () => {
await runInit({
root: paths.root,
contracts: [`${paths.contractsBarDir}/*`],
output: paths.outputDir,
});

const [relativeBarDir] = [paths.contractsBarDir.replace(paths.workspaceDir, 'workspace')];

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();

const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({

Check failure on line 104 in packages/fuels/test/features/init.test.ts

View workflow job for this annotation

GitHub Actions / node@18

packages/fuels/test/features/init.test.ts > init > should run `init` command with --contracts [glob path - single path]

AssertionError: expected { contracts: [ …(2) ], …(1) } to deeply equal { …(2) } - Expected + Received Object { "contracts": Array [ - "workspace/contracts/bar", + "workspace/contracts/bar/src", + "workspace/contracts/bar/Forc.toml", ], "output": "./output", } ❯ packages/fuels/test/features/init.test.ts:104:25

Check failure on line 104 in packages/fuels/test/features/init.test.ts

View workflow job for this annotation

GitHub Actions / node@20

packages/fuels/test/features/init.test.ts > init > should run `init` command with --contracts [glob path - single path]

AssertionError: expected { contracts: [ …(2) ], …(1) } to deeply equal { …(2) } - Expected + Received Object { "contracts": Array [ - "workspace/contracts/bar", + "workspace/contracts/bar/src", + "workspace/contracts/bar/Forc.toml", ], "output": "./output", } ❯ packages/fuels/test/features/init.test.ts:104:25

Check failure on line 104 in packages/fuels/test/features/init.test.ts

View workflow job for this annotation

GitHub Actions / node@22

packages/fuels/test/features/init.test.ts > init > should run `init` command with --contracts [glob path - single path]

AssertionError: expected { contracts: [ …(2) ], …(1) } to deeply equal { …(2) } - Expected + Received Object { "contracts": Array [ - "workspace/contracts/bar", + "workspace/contracts/bar/src", + "workspace/contracts/bar/Forc.toml", ], "output": "./output", } ❯ packages/fuels/test/features/init.test.ts:104:25
contracts: [relativeBarDir],
output: './output',
});
});

it('should run `init` command with --predicates', async () => {
Expand All @@ -75,9 +117,11 @@
const relativePredicateDir = paths.predicateDir.replace(paths.workspaceDir, 'workspace');

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();
const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8');
expect(fuelsContents).toMatch(/predicates:/);
expect(fuelsContents).toMatch(relativePredicateDir);
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
predicates: [relativePredicateDir],
output: './output',
});
});

it('should run `init` command with --scripts', async () => {
Expand All @@ -90,9 +134,11 @@
const relativeScriptDir = paths.scriptsDir.replace(paths.workspaceDir, 'workspace');

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();
const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8');
expect(fuelsContents).toMatch(/scripts:/);
expect(fuelsContents).toMatch(relativeScriptDir);
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
scripts: [relativeScriptDir],
output: './output',
});
});

it('should run `init` command using custom binaries', async () => {
Expand All @@ -105,11 +151,13 @@
});

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();
const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8');
expect(fuelsContents).toMatch(`workspace: './workspace',`);
expect(fuelsContents).toMatch(`output: './output',`);
expect(fuelsContents).toMatch(`forcPath: 'fuels-forc',`);
expect(fuelsContents).toMatch(`fuelCorePath: 'fuels-core',`);
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
workspace: './workspace',
output: './output',
forcPath: 'fuels-forc',
fuelCorePath: 'fuels-core',
});
});

it('should run `init` command with custom fuel-core-port', async () => {
Expand All @@ -120,8 +168,12 @@
fuelCorePort: '1234',
});

const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8');
expect(fuelsContents).toMatch(`fuelCorePort: 1234,`);
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
fuelCorePort: 1234,
output: './output',
workspace: './workspace',
});
});

it('should run `init` command and throw for existent config file', async () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/fuels/test/utils/runCommands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { cpSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs';
import { join, basename } from 'path';

import type { FuelsConfig } from '../../src';
import { Commands } from '../../src';
import { run } from '../../src/run';

Expand Down Expand Up @@ -187,3 +188,12 @@ export function resetDiskAndMocks(dirPath: string) {
}
vi.restoreAllMocks();
}

/**
* Loaders
*/
export async function loadFuelsConfig(configPath: string): Promise<FuelsConfig> {
const configPathWithCacheBust = `${configPath}?update=${Date.now()}`;
const { default: fuelsConfig } = await import(configPathWithCacheBust);
return fuelsConfig;
}
Loading