Skip to content

Commit

Permalink
refactor: create function to set up test repo
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Aug 12, 2024
1 parent 66136e8 commit eede089
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 39 deletions.
17 changes: 4 additions & 13 deletions test/commands/delta/empty.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
'use strict';

import { promises as fsPromises, readFile, stat, readdir, mkdtempSync } from 'node:fs';
import { mkdir, writeFile, rm } from 'node:fs/promises';
import git from 'isomorphic-git';
import { rm } from 'node:fs/promises';

import { TestContext } from '@salesforce/core/lib/testSetup.js';
import { expect } from 'chai';
import { stubSfCommandUx } from '@salesforce/sf-plugins-core';
import ApexTestDelta from '../../../src/commands/apex-tests-git-delta/delta.js';
import { createTemporaryCommit } from './createTemporaryCommit.js';
import { regExFile, regExFileContents, sfdxConfigFile, sfdxConfigJsonString } from './testConstants.js';
import { setupTestRepo } from './setupTestRepo.js';

describe('scan commit messages without the regex and return an empty string.', () => {
const $$ = new TestContext();
let sfCommandStubs: ReturnType<typeof stubSfCommandUx>;
let fromSha: string;
let toSha: string;
let tempDir: string;
const originalDir = process.cwd();
const tempDir = mkdtempSync('../git-temp-');
const fs = { promises: fsPromises, readFile, stat, readdir };

before(async () => {
process.chdir(tempDir);

await mkdir('force-app/main/default/classes', { recursive: true });
await mkdir('packaged/classes', { recursive: true });
await git.init({ fs, dir: tempDir });
await writeFile(regExFile, regExFileContents);
await writeFile(sfdxConfigFile, sfdxConfigJsonString);
tempDir = await setupTestRepo();

fromSha = await createTemporaryCommit(
'chore: initial commit',
Expand Down
20 changes: 20 additions & 0 deletions test/commands/delta/setupTestRepo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

import { promises as fsPromises, readFile, stat, readdir, mkdtempSync } from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
import git from 'isomorphic-git';

import { regExFile, regExFileContents, sfdxConfigFile, sfdxConfigJsonString } from './testConstants.js';

export async function setupTestRepo(): Promise<string> {
const tempDir = mkdtempSync('../git-temp-');
process.chdir(tempDir);
const fs = { promises: fsPromises, readFile, stat, readdir };

await mkdir('force-app/main/default/classes', { recursive: true });
await mkdir('packaged/classes', { recursive: true });
await git.init({ fs, dir: tempDir });
await writeFile(regExFile, regExFileContents);
await writeFile(sfdxConfigFile, sfdxConfigJsonString);
return tempDir;
}
17 changes: 4 additions & 13 deletions test/commands/delta/unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
'use strict';

import { promises as fsPromises, readFile, stat, readdir, mkdtempSync } from 'node:fs';
import { mkdir, writeFile, rm } from 'node:fs/promises';
import git from 'isomorphic-git';
import { rm } from 'node:fs/promises';

import { TestContext } from '@salesforce/core/lib/testSetup.js';
import { expect } from 'chai';
import { stubSfCommandUx } from '@salesforce/sf-plugins-core';
import ApexTestDelta from '../../../src/commands/apex-tests-git-delta/delta.js';
import { createTemporaryCommit } from './createTemporaryCommit.js';
import { regExFile, regExFileContents, sfdxConfigFile, sfdxConfigJsonString } from './testConstants.js';
import { setupTestRepo } from './setupTestRepo.js';

describe('return the delta tests between git commits', () => {
const $$ = new TestContext();
let sfCommandStubs: ReturnType<typeof stubSfCommandUx>;
let fromSha: string;
let toSha: string;
let tempDir: string;
const originalDir = process.cwd();
const tempDir = mkdtempSync('../git-temp-');
const fs = { promises: fsPromises, readFile, stat, readdir };

before(async () => {
process.chdir(tempDir);

await mkdir('force-app/main/default/classes', { recursive: true });
await mkdir('packaged/classes', { recursive: true });
await git.init({ fs, dir: tempDir });
await writeFile(regExFile, regExFileContents);
await writeFile(sfdxConfigFile, sfdxConfigJsonString);
tempDir = await setupTestRepo();

fromSha = await createTemporaryCommit(
'chore: initial commit with Apex::TestClass00::Apex',
Expand Down
17 changes: 4 additions & 13 deletions test/commands/delta/warnings.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
'use strict';

import { promises as fsPromises, readFile, stat, readdir, mkdtempSync } from 'node:fs';
import { mkdir, writeFile, rm } from 'node:fs/promises';
import git from 'isomorphic-git';
import { rm } from 'node:fs/promises';

import { TestContext } from '@salesforce/core/lib/testSetup.js';
import { expect } from 'chai';
import { stubSfCommandUx } from '@salesforce/sf-plugins-core';
import ApexTestDelta from '../../../src/commands/apex-tests-git-delta/delta.js';
import { createTemporaryCommit } from './createTemporaryCommit.js';
import { regExFile, regExFileContents, sfdxConfigFile, sfdxConfigJsonString } from './testConstants.js';
import { setupTestRepo } from './setupTestRepo.js';

describe('confirm warnings are generated when files cannot be found in a package directory.', () => {
const $$ = new TestContext();
let sfCommandStubs: ReturnType<typeof stubSfCommandUx>;
let fromSha: string;
let toSha: string;
let tempDir: string;
const originalDir = process.cwd();
const tempDir = mkdtempSync('../git-temp-');
const fs = { promises: fsPromises, readFile, stat, readdir };

before(async () => {
process.chdir(tempDir);

await mkdir('force-app/main/default/classes', { recursive: true });
await mkdir('packaged/classes', { recursive: true });
await git.init({ fs, dir: tempDir });
await writeFile(regExFile, regExFileContents);
await writeFile(sfdxConfigFile, sfdxConfigJsonString);
tempDir = await setupTestRepo();

fromSha = await createTemporaryCommit(
'chore: initial commit with Apex::TestClass00::Apex',
Expand Down

0 comments on commit eede089

Please sign in to comment.