Skip to content

Commit

Permalink
test: remove fs-extra
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Jun 24, 2024
1 parent 050b2bb commit a82e033
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 104 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@salesforce/prettier-config": "^0.0.3",
"@types/chai-as-promised": "^7.1.8",
"@types/ejs": "^3.1.5",
"@types/fs-extra": "^11.0.4",
"@types/got": "^9.6.12",
"@types/mime-types": "^2.1.4",
"@types/proxy-from-env": "^1.0.4",
Expand All @@ -49,7 +48,6 @@
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-jsdoc": "^46.8.2",
"eslint-plugin-prettier": "^3.1.3",
"fs-extra": "^11.2.0",
"husky": "^8.0.3",
"mocha": "^10.4.0",
"nyc": "^15.1.0",
Expand Down
131 changes: 53 additions & 78 deletions test/service/templateService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as fsOriginal from 'fs';
import * as fs from 'fs-extra';
import * as fs from 'fs';
import { rm } from 'node:fs/promises';
import got from 'got';
import * as path from 'path';
import { assert, spy, stub, createSandbox } from 'sinon';
import * as sinon from 'sinon';
import { TemplateService, TemplateType } from '../../src';
import { nls } from '../../src/i18n';
import { getStoragePathForCustomTemplates } from '../../src/service/gitRepoUtils';
Expand All @@ -27,6 +27,10 @@ chai.use(chaiAsPromised);
chai.config.truncateThreshold = 100000;
chai.should();

async function remove(file: string) {
await rm(file, { force: true, recursive: true });
}

function assertFileContent(file: string, regex: string | RegExp) {
const exists = fs.existsSync(file);
chai.expect(exists).to.be.true;
Expand Down Expand Up @@ -103,21 +107,19 @@ describe('TemplateService', () => {
});

describe('create custom template', () => {
// add reference to a sinon sandbox
const sandbox = createSandbox();
const TEST_CUSTOM_TEMPLATES_REPO =
'https://github.com/forcedotcom/salesforcedx-templates/tree/main/test/custom-templates';
const TEST_CUSTOM_TEMPLATES_STORAGE_PATH = getStoragePathForCustomTemplates(
new URL(TEST_CUSTOM_TEMPLATES_REPO)
);

beforeEach(async () => {
await fs.remove(
await remove(
path.join('testsoutput', 'customLibraryCreate', 'apexclass')
);
});
afterEach(() => {
sandbox.restore();
sinon.restore();
});

it('should create custom template from local folder', async () => {
Expand Down Expand Up @@ -166,9 +168,8 @@ describe('TemplateService', () => {
it('should create custom template from GitHub repository', async () => {
const templateService = TemplateService.getInstance(process.cwd());
const customTemplates = TEST_CUSTOM_TEMPLATES_REPO;
if (fs.existsSync(TEST_CUSTOM_TEMPLATES_STORAGE_PATH)) {
fs.removeSync(TEST_CUSTOM_TEMPLATES_STORAGE_PATH);
}
await remove(TEST_CUSTOM_TEMPLATES_STORAGE_PATH);

await templateService.create(
TemplateType.ApexClass,
{
Expand Down Expand Up @@ -238,7 +239,7 @@ describe('TemplateService', () => {
getProxyForUrl,
};
// @ts-ignore - function signature is not compatible with sinon stub
sandbox.stub(mockIt, 'getProxyForUrl').returns(undefined);
sinon.stub(mockIt, 'getProxyForUrl').returns(undefined);
const templateService = TemplateService.getInstance(process.cwd());
const customTemplates =
'https://github.com/forcedotcom/this-repo-does-not-exist';
Expand Down Expand Up @@ -342,61 +343,42 @@ describe('TemplateService', () => {
});

it('should download the repo if the folder does not exist', async () => {
const existsSyncStub = stub(fsOriginal, 'existsSync');
existsSyncStub.callsFake((fsPath) => {
if (fsPath === TEST_CUSTOM_TEMPLATES_REPO) {
return true;
}
return fs.existsSync(fsPath);
});
const streamStub = spy(got, 'stream');
if (fs.existsSync(TEST_CUSTOM_TEMPLATES_STORAGE_PATH)) {
fs.removeSync(TEST_CUSTOM_TEMPLATES_STORAGE_PATH);
}
sinon
.stub(fs, 'existsSync')
.withArgs(TEST_CUSTOM_TEMPLATES_STORAGE_PATH)
.returns(false);
const streamStub = sinon.spy(got, 'stream');
await remove(TEST_CUSTOM_TEMPLATES_STORAGE_PATH);
const customTemplates = TEST_CUSTOM_TEMPLATES_REPO;

await setCustomTemplatesRootPathOrGitRepo(customTemplates);

assert.callCount(streamStub, 1);
streamStub.restore();
existsSyncStub.restore();
sinon.assert.callCount(streamStub, 1);
});

it('should not download the repo if the folder already exists', async () => {
const existsSyncStub = stub(fsOriginal, 'existsSync');
existsSyncStub.callsFake((fsPath) => {
if (fsPath === TEST_CUSTOM_TEMPLATES_REPO) {
return true;
}
return fs.existsSync(fsPath);
});
const streamStub = spy(got, 'stream');
sinon
.stub(fs, 'existsSync')
.withArgs(TEST_CUSTOM_TEMPLATES_STORAGE_PATH)
.returns(true);
const streamStub = sinon.spy(got, 'stream');

if (fs.existsSync(TEST_CUSTOM_TEMPLATES_STORAGE_PATH)) {
fs.removeSync(TEST_CUSTOM_TEMPLATES_STORAGE_PATH);
}
await remove(TEST_CUSTOM_TEMPLATES_STORAGE_PATH);
fs.mkdirSync(TEST_CUSTOM_TEMPLATES_STORAGE_PATH, { recursive: true });
const customTemplates = TEST_CUSTOM_TEMPLATES_REPO;

await setCustomTemplatesRootPathOrGitRepo(customTemplates);

assert.notCalled(streamStub);
streamStub.restore();
existsSyncStub.restore();
sinon.assert.notCalled(streamStub);
});

it('should download the repo if the folder already exists, but forcing a redownload', async () => {
const existsSyncStub = stub(fsOriginal, 'existsSync');
existsSyncStub.callsFake((fsPath) => {
if (fsPath === TEST_CUSTOM_TEMPLATES_REPO) {
return true;
}
return fs.existsSync(fsPath);
});
const streamStub = spy(got, 'stream');
if (fs.existsSync(TEST_CUSTOM_TEMPLATES_STORAGE_PATH)) {
fs.removeSync(TEST_CUSTOM_TEMPLATES_STORAGE_PATH);
}
sinon
.stub(fs, 'existsSync')
.withArgs(TEST_CUSTOM_TEMPLATES_STORAGE_PATH)
.returns(true);
const streamStub = sinon.spy(got, 'stream');
await remove(TEST_CUSTOM_TEMPLATES_STORAGE_PATH);
fs.mkdirSync(TEST_CUSTOM_TEMPLATES_STORAGE_PATH, { recursive: true });
const customTemplates = TEST_CUSTOM_TEMPLATES_REPO;

Expand All @@ -406,15 +388,16 @@ describe('TemplateService', () => {
forceLoadingRemoteRepo
);

assert.calledOnce(streamStub);
streamStub.restore();
existsSyncStub.restore();
sinon.assert.calledOnce(streamStub);
});
}).timeout(20000);

describe('create template', () => {
afterEach(() => {
sinon.restore();
});
it('create template should return created output', async () => {
await fs.remove(path.join('testsoutput', 'libraryCreate', 'apexClass'));
await remove(path.join('testsoutput', 'libraryCreate', 'apexClass'));
const templateService = TemplateService.getInstance(process.cwd());
const result = await templateService.create(TemplateType.ApexClass, {
template: 'DefaultApexClass',
Expand Down Expand Up @@ -458,9 +441,7 @@ describe('TemplateService', () => {
});

it('should reject if create template fails', async () => {
const generatorStub = stub(BaseGenerator.prototype, 'run').throws(
new Error('error')
);
sinon.stub(BaseGenerator.prototype, 'run').throws(new Error('error'));
const templateService = TemplateService.getInstance(process.cwd());
try {
await templateService.create(TemplateType.ApexClass, {
Expand All @@ -472,8 +453,6 @@ describe('TemplateService', () => {
} catch (error) {
const err = error as Error;
chai.expect(err.message).to.equal('error');
} finally {
generatorStub.restore();
}
});
});
Expand All @@ -500,9 +479,7 @@ describe('TemplateService', () => {
});

it('should create AnalyticsTemplate', async () => {
await fs.remove(
path.join('testsoutput', 'libraryCreate', 'waveTemplates')
);
await remove(path.join('testsoutput', 'libraryCreate', 'waveTemplates'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(
TemplateType.AnalyticsTemplate,
Expand Down Expand Up @@ -531,7 +508,7 @@ describe('TemplateService', () => {
});

it('should create ApexClass', async () => {
await fs.remove(path.join('testsoutput', 'libraryCreate', 'apexClass'));
await remove(path.join('testsoutput', 'libraryCreate', 'apexClass'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(TemplateType.ApexClass, {
template: 'DefaultApexClass',
Expand All @@ -552,7 +529,7 @@ describe('TemplateService', () => {
});

it('should create ApexTrigger', async () => {
await fs.remove(path.join('testsoutput', 'libraryCreate', 'apexTrigger'));
await remove(path.join('testsoutput', 'libraryCreate', 'apexTrigger'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(TemplateType.ApexTrigger, {
triggername: 'LibraryCreateTrigger',
Expand All @@ -575,7 +552,7 @@ describe('TemplateService', () => {
});

it('should create LightningApp', async () => {
await fs.remove(path.join('testsoutput', 'libraryCreate', 'aura'));
await remove(path.join('testsoutput', 'libraryCreate', 'aura'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(TemplateType.LightningApp, {
appname: 'LibraryCreateApp',
Expand Down Expand Up @@ -603,7 +580,7 @@ describe('TemplateService', () => {
});

it('should create LightningComponent (aura)', async () => {
await fs.remove(path.join('testsoutput', 'libraryCreate', 'aura'));
await remove(path.join('testsoutput', 'libraryCreate', 'aura'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(
TemplateType.LightningComponent,
Expand Down Expand Up @@ -635,7 +612,7 @@ describe('TemplateService', () => {
});

it('should create LightningComponent (lwc)', async () => {
await fs.remove(path.join('testsoutput', 'libraryCreate', 'lwc'));
await remove(path.join('testsoutput', 'libraryCreate', 'lwc'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(
TemplateType.LightningComponent,
Expand All @@ -662,7 +639,7 @@ describe('TemplateService', () => {
});

it('should create LightningEvent', async () => {
await fs.remove(path.join('testsoutput', 'libraryCreate', 'aura'));
await remove(path.join('testsoutput', 'libraryCreate', 'aura'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(TemplateType.LightningEvent, {
eventname: 'LibraryCreateEvent',
Expand All @@ -683,7 +660,7 @@ describe('TemplateService', () => {
});

it('should create LightningInterface', async () => {
await fs.remove(path.join('testsoutput', 'libraryCreate', 'aura'));
await remove(path.join('testsoutput', 'libraryCreate', 'aura'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(
TemplateType.LightningInterface,
Expand All @@ -707,7 +684,7 @@ describe('TemplateService', () => {
});

it('should create LightningTest', async () => {
await fs.remove(path.join('testsoutput', 'libraryCreate', 'aura'));
await remove(path.join('testsoutput', 'libraryCreate', 'aura'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(TemplateType.LightningTest, {
testname: 'LibraryCreateTest',
Expand All @@ -728,7 +705,7 @@ describe('TemplateService', () => {
});

it('should create Project (standard)', async () => {
await fs.remove(path.join('testsoutput', 'libraryCreate', 'project'));
await remove(path.join('testsoutput', 'libraryCreate', 'project'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(TemplateType.Project, {
outputdir: path.join('testsoutput', 'libraryCreate', 'project'),
Expand Down Expand Up @@ -765,7 +742,7 @@ describe('TemplateService', () => {
});

it('should create Project (empty)', async () => {
await fs.remove(path.join('testsoutput', 'libraryCreate', 'project'));
await remove(path.join('testsoutput', 'libraryCreate', 'project'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(TemplateType.Project, {
outputdir: path.join('testsoutput', 'libraryCreate', 'project'),
Expand All @@ -789,7 +766,7 @@ describe('TemplateService', () => {
});

it('should create Project (analytics)', async () => {
await fs.remove(path.join('testsoutput', 'libraryCreate', 'project'));
await remove(path.join('testsoutput', 'libraryCreate', 'project'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(TemplateType.Project, {
outputdir: path.join('testsoutput', 'libraryCreate', 'project'),
Expand Down Expand Up @@ -824,9 +801,7 @@ describe('TemplateService', () => {
});

it('should create StaticResource', async () => {
await fs.remove(
path.join('testsoutput', 'libraryCreate', 'staticResource')
);
await remove(path.join('testsoutput', 'libraryCreate', 'staticResource'));
const templateService = TemplateService.getInstance();
const result = await templateService.create(TemplateType.StaticResource, {
resourcename: 'LibraryCreateResource',
Expand All @@ -848,7 +823,7 @@ describe('TemplateService', () => {
});

it('should create VisualforceComponent', async () => {
await fs.remove(
await remove(
path.join('testsoutput', 'libraryCreate', 'visualforceComponent')
);
const templateService = TemplateService.getInstance();
Expand Down Expand Up @@ -879,7 +854,7 @@ describe('TemplateService', () => {
});

it('should create VisualforcePage', async () => {
await fs.remove(
await remove(
path.join('testsoutput', 'libraryCreate', 'visualforcePage')
);
const templateService = TemplateService.getInstance();
Expand Down
24 changes: 0 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -868,14 +868,6 @@
resolved "https://registry.npmjs.org/@types/expect/-/expect-1.20.4.tgz"
integrity sha512-Q5Vn3yjTDyCMV50TB6VRIbQNxSE4OmZR86VSbGaNpfUolm0iePBB4KdEEHmxoY5sT2+2DIvXW0rvMDP2nHZ4Mg==

"@types/fs-extra@^11.0.4":
version "11.0.4"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-11.0.4.tgz#e16a863bb8843fba8c5004362b5a73e17becca45"
integrity sha512-yTbItCNreRooED33qjunPthRcSjERP1r4MqCZc7wv0u2sUkzTFp45tgUfS5+r7FrZPdmCCNflLhVSP/o+SemsQ==
dependencies:
"@types/jsonfile" "*"
"@types/node" "*"

"@types/glob@*":
version "7.2.0"
resolved "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz"
Expand Down Expand Up @@ -916,13 +908,6 @@
resolved "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz"
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==

"@types/jsonfile@*":
version "6.1.4"
resolved "https://registry.yarnpkg.com/@types/jsonfile/-/jsonfile-6.1.4.tgz#614afec1a1164e7d670b4a7ad64df3e7beb7b702"
integrity sha512-D5qGUYwjvnNNextdU59/+fI+spnwtTFmyQP0h+PfIOSkNfpU6AOICUOkm4i0OnSk+NyjdPJrxCDro0sJsWlRpQ==
dependencies:
"@types/node" "*"

"@types/keyv@^3.1.4":
version "3.1.4"
resolved "https://registry.yarnpkg.com/@types/keyv/-/keyv-3.1.4.tgz#3ccdb1c6751b0c7e52300bcdacd5bcbf8faa75b6"
Expand Down Expand Up @@ -3013,15 +2998,6 @@ fs-extra@^11.0.0:
jsonfile "^6.0.1"
universalify "^2.0.0"

fs-extra@^11.2.0:
version "11.2.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-11.2.0.tgz#e70e17dfad64232287d01929399e0ea7c86b0e5b"
integrity sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw==
dependencies:
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^2.0.0"

fs-extra@^8.1.0:
version "8.1.0"
resolved "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz"
Expand Down

0 comments on commit a82e033

Please sign in to comment.