Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@salesforce/source-testkit",
"description": "testkit for running NUTs for source related commands",
"version": "0.0.18",
"version": "1.0.0",
"author": "Salesforce",
"license": "BSD-3-Clause",
"main": "lib/index.js",
Expand Down Expand Up @@ -30,26 +30,26 @@
"!lib/**/*.map"
],
"dependencies": {
"@salesforce/cli-plugins-testkit": "^1.2.0",
"@salesforce/core": "^2.20.11",
"@salesforce/cli-plugins-testkit": "^2.0.0",
"@salesforce/core": "^3.19.1",
"@salesforce/kit": "^1.4.5",
"@salesforce/source-deploy-retrieve": "^4.0.2",
"@salesforce/source-deploy-retrieve": "^6.0.2",
"@salesforce/ts-types": "^1.5.5",
"archiver": "^5.2.0",
"chai-each": "^0.0.1",
"debug": "^4.3.1",
"shelljs": "^0.8.4",
"sinon": "^10.0.0",
"strip-ansi": "^6.0.0"
"strip-ansi": "^7.0.1"
},
"devDependencies": {
"@salesforce/dev-config": "^3.0.0",
"@salesforce/dev-scripts": "^2.0.0",
"@salesforce/dev-scripts": "^2.0.2",
"@salesforce/prettier-config": "^0.0.2",
"@salesforce/ts-sinon": "^1.3.5",
"@types/archiver": "^5.1.0",
"@types/debug": "^4.1.5",
"@types/shelljs": "^0.8.8",
"@types/shelljs": "^0.8.11",
"@typescript-eslint/eslint-plugin": "^4.15.2",
"@typescript-eslint/parser": "^4.15.2",
"chai": "^4.3.0",
Expand All @@ -59,7 +59,7 @@
"eslint-config-prettier": "^8.1.0",
"eslint-config-salesforce": "^0.1.6",
"eslint-config-salesforce-license": "^0.1.6",
"eslint-config-salesforce-typescript": "^0.2.7",
"eslint-config-salesforce-typescript": "^0.2.8",
"eslint-plugin-header": "^3.1.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsdoc": "^32.2.0",
Expand All @@ -71,7 +71,7 @@
"pretty-quick": "^3.1.0",
"sinon": "^10.0.0",
"ts-node": "^10.0.0",
"typescript": "^4.1.3"
"typescript": "^4.7.2"
},
"config": {
"commitizen": {
Expand Down
10 changes: 5 additions & 5 deletions src/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import * as fs from 'fs';
import * as path from 'path';
import { expect, use } from 'chai';
import * as chaiEach from 'chai-each';
import { JsonMap, Nullable } from '@salesforce/ts-types';
import * as fg from 'fast-glob';
import { Connection, fs } from '@salesforce/core';
import { Connection } from '@salesforce/core';
import { FileResponse, MetadataResolver } from '@salesforce/source-deploy-retrieve';
import { debug, Debugger } from 'debug';
import { ApexClass, ApexTestResult, Commands, Context, SourceMember, SourceState, StatusResult } from './types';
Expand Down Expand Up @@ -157,7 +157,7 @@ export class Assertions {
*/
public async fileToExist(file: string): Promise<void> {
const fullPath = file.startsWith(this.projectDir) ? file : path.join(this.projectDir, file);
const fileExists = await fs.fileExists(fullPath);
const fileExists = fs.existsSync(fullPath);
expect(fileExists, `${fullPath} to exist`).to.be.true;
}

Expand Down Expand Up @@ -197,7 +197,7 @@ export class Assertions {
public async filesToNotContainString(glob: string, ...strings: string[]): Promise<void> {
const files = await this.doGlob([glob]);
for (const file of files) {
const contents = await fs.readFile(file, 'UTF-8');
const contents = await fs.promises.readFile(file, 'utf-8');
for (const str of strings) {
expect(contents, `expect ${file} to not include ${str}`).to.not.include(str);
}
Expand All @@ -210,7 +210,7 @@ export class Assertions {
public async filesToContainString(glob: string, ...strings: string[]): Promise<void> {
const files = await this.doGlob([glob]);
for (const file of files) {
const contents = await fs.readFile(file, 'UTF-8');
const contents = await fs.promises.readFile(file, 'utf-8');
for (const str of strings) {
expect(contents, `expect ${file} to not include ${str}`).to.include(str);
}
Expand Down
14 changes: 8 additions & 6 deletions src/fileTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import * as fs from 'fs';
import * as path from 'path';
import { fs } from '@salesforce/core';
import * as crypto from 'crypto';
import { Nullable } from '@salesforce/ts-types';
import { Context } from './types';

Expand Down Expand Up @@ -82,10 +82,12 @@ export class FileTracker {
private async getContentHash(file: string): Promise<Nullable<string>> {
const filePath = this.getFullPath(file);
try {
const filestat = await fs.stat(filePath);
const filestat = await fs.promises.stat(filePath);
const isDirectory = filestat.isDirectory();
const contents = isDirectory ? (await fs.readdir(filePath)).toString() : await fs.readFile(filePath);
return fs.getContentHash(contents);
const contents = isDirectory
? (await fs.promises.readdir(filePath)).toString()
: await fs.promises.readFile(filePath);
return crypto.createHash('sha1').update(contents).digest('hex');
} catch {
return null;
}
Expand All @@ -109,7 +111,7 @@ export namespace FileTracker {
* Returns all files in directory that match the filter
*/
export async function traverseForFiles(dirPath: string, regexFilter = /./, allFiles: string[] = []): Promise<string[]> {
const files = await fs.readdir(dirPath);
const files = await fs.promises.readdir(dirPath);

for (const file of files) {
const filePath = path.join(dirPath, file);
Expand Down
41 changes: 21 additions & 20 deletions src/testkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@

import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';
import * as fg from 'fast-glob';
import { exec, find, mv, rm, which } from 'shelljs';
import { TestSession, execCmd } from '@salesforce/cli-plugins-testkit';
import { AsyncCreatable, Env, set } from '@salesforce/kit';
import { AsyncCreatable, Env, set, parseJsonMap } from '@salesforce/kit';
import { AnyJson, Dictionary, ensureString, get, JsonMap, Nullable } from '@salesforce/ts-types';
import { AuthInfo, Config, Connection, fs, NamedPackageDir, SfdxProject } from '@salesforce/core';
import { AuthInfo, SfdxPropertyKeys, Connection, NamedPackageDir, SfdxProject } from '@salesforce/core';
import { debug, Debugger } from 'debug';
import { MetadataResolver } from '@salesforce/source-deploy-retrieve';
import { Commands, Result, StatusResult } from './types';
Expand Down Expand Up @@ -74,7 +75,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {

public constructor(options: SourceTestkit.Options) {
super(options);
this.executable = options.executable || which('sfdx').stdout;
this.executable = options.executable ?? which('sfdx')?.stdout ?? 'sfdx';
this.repository = options.repository;
this.orgless = !!options.orgless;
this.setupCommands = options.setupCommands || [];
Expand Down Expand Up @@ -209,7 +210,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
const files = await this.doGlob(globs);
const returnValue: Dictionary<string> = {};
for (const file of files) {
returnValue[file] = await fs.readFile(file, 'UTF-8');
returnValue[file] = await fs.promises.readFile(file, 'utf8');
}
return returnValue;
}
Expand All @@ -219,30 +220,30 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
*/
public async readSourcePathInfos(): Promise<AnyJson> {
const sourcePathInfosPath = path.join(this.projectDir, '.sfdx', 'orgs', this.username, 'sourcePathInfos.json');
return fs.readJson(sourcePathInfosPath);
return JSON.parse(await fs.promises.readFile(sourcePathInfosPath, 'utf8')) as AnyJson;
}

/**
* Read the org's maxRevision.json
*/
public async readMaxRevision(): Promise<{ sourceMembers: JsonMap }> {
const maxRevisionPath = path.join(this.projectDir, '.sfdx', 'orgs', this.username, 'maxRevision.json');
return fs.readJson(maxRevisionPath) as unknown as { sourceMembers: JsonMap };
return JSON.parse(await fs.promises.readFile(maxRevisionPath, 'utf8')) as { sourceMembers: JsonMap };
}

/**
* Write the org's maxRevision.json
*/
public async writeMaxRevision(contents: JsonMap): Promise<void> {
const maxRevisionPath = path.join(this.projectDir, '.sfdx', 'orgs', this.username, 'maxRevision.json');
return fs.writeJson(maxRevisionPath, contents);
return fs.promises.writeFile(maxRevisionPath, JSON.stringify(contents));
}

/**
* Write file
*/
public async writeFile(filename: string, contents: string): Promise<void> {
return fs.writeFile(filename, contents);
return fs.promises.writeFile(filename, contents);
}

/**
Expand All @@ -256,7 +257,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
</Package>
`;
const packageXmlPath = path.join(this.projectDir, 'package.xml');
await fs.writeFile(packageXmlPath, packageXml);
await fs.promises.writeFile(packageXmlPath, packageXml);
return packageXmlPath;
}

Expand All @@ -265,15 +266,15 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
*/
public async deleteSourcePathInfos(): Promise<void> {
const sourcePathInfosPath = path.join(this.projectDir, '.sfdx', 'orgs', this.username, 'sourcePathInfos.json');
return fs.unlink(sourcePathInfosPath);
return fs.promises.unlink(sourcePathInfosPath);
}

/**
* Delete the org's maxRevision.json
*/
public async deleteMaxRevision(): Promise<void> {
const maxRevisionPath = path.join(this.projectDir, '.sfdx', 'orgs', this.username, 'maxRevision.json');
return fs.unlink(maxRevisionPath);
return fs.promises.unlink(maxRevisionPath);
}

/**
Expand All @@ -282,7 +283,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
public async deleteGlobs(globs: string[]): Promise<void> {
const files = await this.doGlob(globs);
for (const file of files) {
await fs.unlink(file);
await fs.promises.unlink(file);
}
}

Expand All @@ -291,8 +292,8 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
*/
public async deleteAllSourceFiles(): Promise<void> {
for (const pkg of this.packagePaths) {
await fs.rmdir(pkg, { recursive: true });
await fs.mkdirp(pkg);
await fs.promises.rm(pkg, { recursive: true });
await fs.promises.mkdir(pkg, { recursive: true });
}
}

Expand All @@ -312,9 +313,9 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
*/
public async modifyLocalFile(file: string, append = os.EOL): Promise<void> {
const fullPath = file.startsWith(this.projectDir) ? file : path.join(this.projectDir, file);
let contents = await fs.readFile(fullPath, 'UTF-8');
let contents = await fs.promises.readFile(fullPath, 'utf-8');
contents += append;
await fs.writeFile(fullPath, contents);
await fs.promises.writeFile(fullPath, contents);
await this.fileTracker.update(fullPath, 'modified file');
}

Expand All @@ -330,7 +331,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
.sobject('QuickActionDefinition')
.find({ DeveloperName: 'NutAction' }, ['ID']))!;
const updateRequest = {
Id: result[0].Id,
Id: result[0].Id as string,
Description: 'updated description',
};
await this.connection?.tooling.sobject('QuickActionDefinition').update(updateRequest);
Expand All @@ -348,7 +349,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
try {
fs.copyFileSync(src, dest);
} catch {
await fs.mkdirp(path.dirname(dest));
await fs.promises.mkdir(path.dirname(dest), { recursive: true });
fs.copyFileSync(src, dest);
}
}
Expand Down Expand Up @@ -488,7 +489,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
.replace(SourceTestkit.LocalProdPath, '')
.replace(SourceTestkit.LocalDevPath, '');
const pkgJsonPath = path.join(npmPackagePath, 'package.json');
const pkgJson = (await fs.readJsonMap(pkgJsonPath)) as { oclif: { bin: string } };
const pkgJson = parseJsonMap<{ oclif: { bin: string } }>(await fs.promises.readFile(pkgJsonPath, 'utf8'));
return pkgJson.oclif.bin as Executable;
} else {
return path.basename(this.executable) as Executable;
Expand Down Expand Up @@ -536,7 +537,7 @@ export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
}

private async getDefaultUsername(): Promise<string> {
const configVar = this.executableName === Executable.SF ? 'target-org' : Config.DEFAULT_USERNAME;
const configVar = this.executableName === Executable.SF ? 'target-org' : SfdxPropertyKeys.DEFAULT_USERNAME;
const configResult = execCmd(`config:get ${configVar} --json`).jsonOutput!;
const results = get(configResult, 'result', configResult) as Array<{ key?: string; name?: string; value: string }>;
const username = results.find((r) => r.key === configVar || r.name === configVar)!.value;
Expand Down
Loading