Skip to content

Commit

Permalink
refactor: add nuts test
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 authored Sep 23, 2024
1 parent 9fc0c62 commit de02bb1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/non-release-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ on:
jobs:
unit-tests:
uses: salesforcecli/github-workflows/.github/workflows/unitTest.yml@main
nuts:
needs: unit-tests
uses: salesforcecli/github-workflows/.github/workflows/nut.yml@main
secrets: inherit
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
fail-fast: false
with:
os: ${{ matrix.os }}
81 changes: 81 additions & 0 deletions test/commands/transformer/transform.nuts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';

import { copyFile, readFile, writeFile, rm, mkdir } from 'node:fs/promises';
import { strictEqual } from 'node:assert';
import { resolve } from 'node:path';

import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
import { expect } from 'chai';

describe('transform NUTs', () => {
let session: TestSession;
const baselineClassPath = resolve('test/baselines/classes/AccountProfile.cls');
const baselineTriggerPath = resolve('test/baselines/triggers/AccountTrigger.trigger');
const deployCoverageNoExts = resolve('test/deploy_coverage_no_file_exts.json');
const deployCoverageWithExts = resolve('test/deploy_coverage_with_file_exts.json');
// const testCoverage = resolve('test/test_coverage.json');
const baselineXmlPath = resolve('test/coverage_baseline.xml');
const testXmlPath1 = resolve('coverage1.xml');
const testXmlPath2 = resolve('coverage2.xml');
// const testXmlPath3 = resolve('coverage3.xml');
const sfdxConfigFile = resolve('sfdx-project.json');

const configFile = {
packageDirectories: [{ path: 'force-app', default: true }, { path: 'packaged' }],
namespace: '',
sfdcLoginUrl: 'https://login.salesforce.com',
sourceApiVersion: '58.0',
};
const configJsonString = JSON.stringify(configFile, null, 2);

before(async () => {
await mkdir('force-app/main/default/classes', { recursive: true });
await mkdir('packaged/triggers', { recursive: true });
await copyFile(baselineClassPath, 'force-app/main/default/classes/AccountProfile.cls');
await copyFile(baselineTriggerPath, 'packaged/triggers/AccountTrigger.trigger');
await writeFile(sfdxConfigFile, configJsonString);
session = await TestSession.create({ devhubAuthStrategy: 'NONE' });
});

after(async () => {
await session?.clean();
await rm('force-app/main/default/classes/AccountProfile.cls');
await rm('packaged/triggers/AccountTrigger.trigger');
await rm('force-app', { recursive: true });
await rm('packaged', { recursive: true });
await rm(testXmlPath1);
await rm(testXmlPath2);
// await rm(testXmlPath3);
await rm(sfdxConfigFile);
});

it('runs transform on the 1st coverage JSON', async () => {
const command = `apex-code-coverage transformer transform --coverage-json ${deployCoverageNoExts} -xml ${testXmlPath1} `;
const output = execCmd(command, { ensureExitCode: 0 }).shellOutput.stdout;

expect(output.replace('\n', '')).to.equal(`The coverage XML has been written to ${testXmlPath1}`);
});

it('runs transform on the 2nd coverage JSON', async () => {
const command = `apex-code-coverage transformer transform --coverage-json ${deployCoverageWithExts} -xml ${testXmlPath2} `;
const output = execCmd(command, { ensureExitCode: 0 }).shellOutput.stdout;

expect(output.replace('\n', '')).to.equal(`The coverage XML has been written to ${testXmlPath2}`);
});

it('confirm the 2 XML files created in the previous tests are the same as the baseline.', async () => {
const xmlContent1 = await readFile(testXmlPath1, 'utf-8');
const xmlContent2 = await readFile(testXmlPath2, 'utf-8');
const baselineXmlContent = await readFile(baselineXmlPath, 'utf-8');
strictEqual(
xmlContent1,
baselineXmlContent,
`File content is different between ${testXmlPath1} and ${baselineXmlPath}`
);
strictEqual(
xmlContent2,
baselineXmlContent,
`File content is different between ${testXmlPath2} and ${baselineXmlPath}`
);
});
});

0 comments on commit de02bb1

Please sign in to comment.