diff --git a/packages/fuels/test/features/init.test.ts b/packages/fuels/test/features/init.test.ts index 0e3d7d0d292..331bed7b2b5 100644 --- a/packages/fuels/test/features/init.test.ts +++ b/packages/fuels/test/features/init.test.ts @@ -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'; @@ -10,6 +10,7 @@ import { runInit, resetDiskAndMocks, resetConfigAndMocks, + loadFuelsConfig, } from '../utils/runCommands'; /** @@ -39,14 +40,14 @@ describe('init', () => { }); 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], @@ -59,10 +60,51 @@ describe('init', () => { ]; 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({ + contracts: [relativeBarDir], + output: './output', + }); }); it('should run `init` command with --predicates', async () => { @@ -75,9 +117,11 @@ describe('init', () => { 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 () => { @@ -90,9 +134,11 @@ describe('init', () => { 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 () => { @@ -105,11 +151,13 @@ describe('init', () => { }); 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 () => { @@ -120,8 +168,12 @@ describe('init', () => { 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 () => { diff --git a/packages/fuels/test/utils/runCommands.ts b/packages/fuels/test/utils/runCommands.ts index f5a052d81a8..3cf6b1ccd76 100644 --- a/packages/fuels/test/utils/runCommands.ts +++ b/packages/fuels/test/utils/runCommands.ts @@ -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'; @@ -187,3 +188,12 @@ export function resetDiskAndMocks(dirPath: string) { } vi.restoreAllMocks(); } + +/** + * Loaders + */ +export async function loadFuelsConfig(configPath: string): Promise { + const configPathWithCacheBust = `${configPath}?update=${Date.now()}`; + const { default: fuelsConfig } = await import(configPathWithCacheBust); + return fuelsConfig; +}