|
| 1 | +import path, { join } from 'node:path'; |
| 2 | +import tmp from 'tmp-promise'; |
| 3 | +import fs from 'node:fs/promises'; |
| 4 | +import fixturify from 'fixturify'; |
| 5 | +import { execa } from 'execa'; |
| 6 | +import { beforeAll, beforeEach, describe, expect, it } from 'vitest'; |
| 7 | + |
| 8 | +import { |
| 9 | + assertGeneratedCorrectly, |
| 10 | + filesMatching, |
| 11 | + SUPPORTED_PACKAGE_MANAGERS, |
| 12 | + safeExecaEnv, |
| 13 | +} from '../helpers.js'; |
| 14 | +const blueprintPath = path.join(__dirname, '../..'); |
| 15 | +let localEmberCli = require.resolve('ember-cli/bin/ember'); |
| 16 | + |
| 17 | +for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) { |
| 18 | + describe(`--no-build with ${packageManager}`, () => { |
| 19 | + let tmpDir: string; |
| 20 | + let addonDir: string; |
| 21 | + let addonName = 'my-addon'; |
| 22 | + |
| 23 | + async function commandSucceeds(command: string) { |
| 24 | + let result = await execa({ |
| 25 | + cwd: addonDir, |
| 26 | + shell: true, |
| 27 | + preferLocal: true, |
| 28 | + extendEnv: false, |
| 29 | + env: safeExecaEnv(), |
| 30 | + // Allows us to not fail yet when the command fails |
| 31 | + // but we'd still fail appropriately with the exitCode check below. |
| 32 | + // When we fail, we want to check for git diffs for debugging purposes. |
| 33 | + reject: false, |
| 34 | + })(command); |
| 35 | + |
| 36 | + if (result.exitCode !== 0) { |
| 37 | + console.log(result); |
| 38 | + console.log(`\n\n${command} exited with code ${result.exitCode}\n\n`); |
| 39 | + console.log(result.stdout); |
| 40 | + console.log(result.stderr); |
| 41 | + console.log(`\n\n git diff \n\n`); |
| 42 | + await execa({ cwd: addonDir, stdio: 'inherit' })`git diff`; |
| 43 | + } |
| 44 | + |
| 45 | + expect(result.exitCode, `\`${command}\` succeeds`).toEqual(0); |
| 46 | + |
| 47 | + return result; |
| 48 | + } |
| 49 | + |
| 50 | + beforeAll(async () => { |
| 51 | + tmpDir = (await tmp.dir()).path; |
| 52 | + addonDir = join(tmpDir, addonName); |
| 53 | + await execa({ |
| 54 | + cwd: tmpDir, |
| 55 | + extendEnv: false, |
| 56 | + })`${localEmberCli} addon ${addonName} -b ${blueprintPath} --skip-npm --prefer-local true --${packageManager} --no-build`; |
| 57 | + // Have to use --force because NPM is *stricter* when you use tags in package.json |
| 58 | + // than pnpm (in that tags don't match any specified stable version) |
| 59 | + if (packageManager === 'npm') { |
| 60 | + await execa({ cwd: addonDir, extendEnv: false })`npm install --force`; |
| 61 | + } else if (packageManager === 'pnpm') { |
| 62 | + await execa({ cwd: addonDir, extendEnv: false })`pnpm install`; |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + it('was generated correctly', async () => { |
| 67 | + assertGeneratedCorrectly({ |
| 68 | + projectRoot: addonDir, |
| 69 | + packageManager, |
| 70 | + typeScript: false, |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + // Tests are additive, so when running them in order, we want to check linting |
| 75 | + // before we add files from fixtures |
| 76 | + it('lints pass', async () => { |
| 77 | + await commandSucceeds(`${packageManager} run lint`); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('with fixture', () => { |
| 81 | + beforeEach(async () => { |
| 82 | + let addonFixture = fixturify.readSync('./fixtures/typescript'); |
| 83 | + fixturify.writeSync(addonDir, addonFixture); |
| 84 | + |
| 85 | + // It's important that we ensure that dist directory is empty for these tests, |
| 86 | + // troll-y things can happen with shared dists |
| 87 | + await fs.rm(join(addonDir, 'dist'), { recursive: true, force: true }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('lint:fix', async () => { |
| 91 | + await commandSucceeds(`${packageManager} run lint:fix`); |
| 92 | + }); |
| 93 | + |
| 94 | + it('test', async () => { |
| 95 | + let testResult = await commandSucceeds(`${packageManager} run test`); |
| 96 | + |
| 97 | + console.log(testResult.stdout); |
| 98 | + expect(testResult.stdout).to.include('# tests 2'); |
| 99 | + expect(testResult.stdout).to.include('# pass 2'); |
| 100 | + expect(testResult.stdout).to.include('# fail 0'); |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | +} |
0 commit comments