Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #787 from sstone1/issue-776-split-connections
Browse files Browse the repository at this point in the history
UserInputUtil.getDirPath should be sync (contributes to #776)
  • Loading branch information
Simon Stone committed Apr 1, 2019
2 parents 783bbe2 + 815fb63 commit 93a76b7
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 26 deletions.
5 changes: 2 additions & 3 deletions client/src/commands/UserInputUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,9 @@ export class UserInputUtil {
* @returns {String} Returns dir.
*
*/
public static async getDirPath(dir: string): Promise<string> {

public static getDirPath(dir: string): string {
if (dir.startsWith('~')) {
dir = await homeDir(dir.replace('~', ''));
dir = homeDir(dir.replace('~', ''));
}
return dir;
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/commands/deleteGatewayCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function deleteGateway(gatewayTreeItem: GatewayTreeItem): Promise<v
}

const extDir: string = vscode.workspace.getConfiguration().get('blockchain.ext.directory');
const homeExtDir: string = await UserInputUtil.getDirPath(extDir);
const homeExtDir: string = UserInputUtil.getDirPath(extDir);
const gatewayPath: string = path.join(homeExtDir, gatewayRegistryEntry.name);
await fs.remove(gatewayPath);

Expand Down
2 changes: 1 addition & 1 deletion client/src/commands/importSmartContractPackageCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function importSmartContractPackageCommand(): Promise<void> {
try {
const extDir: string = vscode.workspace.getConfiguration().get('blockchain.ext.directory');
const pkgDir: string = path.join(extDir, 'packages');
let resolvedPkgDir: string = await UserInputUtil.getDirPath(pkgDir);
let resolvedPkgDir: string = UserInputUtil.getDirPath(pkgDir);
await fs.ensureDir(resolvedPkgDir);

const packageName: string = path.basename(packagePath);
Expand Down
2 changes: 1 addition & 1 deletion client/src/commands/packageSmartContractCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function packageSmartContract(workspace?: vscode.WorkspaceFolder, o
// Determine the directory that will contain the packages and ensure it exists.
const extDir: string = vscode.workspace.getConfiguration().get('blockchain.ext.directory');
const pkgDir: string = path.join(extDir, 'packages');
resolvedPkgDir = await UserInputUtil.getDirPath(pkgDir);
resolvedPkgDir = UserInputUtil.getDirPath(pkgDir);
await fs.ensureDir(resolvedPkgDir);

// Choose the workspace directory.
Expand Down
2 changes: 1 addition & 1 deletion client/src/fabric/FabricGatewayHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class FabricGatewayHelper {
try {

const extDir: string = vscode.workspace.getConfiguration().get('blockchain.ext.directory');
const homeExtDir: string = await UserInputUtil.getDirPath(extDir);
const homeExtDir: string = UserInputUtil.getDirPath(extDir);
const profileDirPath: string = path.join(homeExtDir, gatewayName);
const profileExists: boolean = await fs.pathExists(profileDirPath);

Expand Down
6 changes: 3 additions & 3 deletions client/src/fabric/FabricRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class FabricRuntime extends EventEmitter {

public async getConnectionProfilePath(): Promise<string> {
const extDir: string = vscode.workspace.getConfiguration().get('blockchain.ext.directory');
const homeExtDir: string = await UserInputUtil.getDirPath(extDir);
const homeExtDir: string = UserInputUtil.getDirPath(extDir);
const dir: string = path.join(homeExtDir, this.name);

return path.join(dir, 'connection.json');
Expand Down Expand Up @@ -243,7 +243,7 @@ export class FabricRuntime extends EventEmitter {
const connectionProfile: string = JSON.stringify(connectionProfileObj, null, 4);

const extDir: string = vscode.workspace.getConfiguration().get('blockchain.ext.directory');
const homeExtDir: string = await UserInputUtil.getDirPath(extDir);
const homeExtDir: string = UserInputUtil.getDirPath(extDir);

if (!dir) {
dir = path.join(homeExtDir, this.name);
Expand All @@ -265,7 +265,7 @@ export class FabricRuntime extends EventEmitter {
public async deleteConnectionDetails(outputAdapter: OutputAdapter): Promise<void> {

const extDir: string = vscode.workspace.getConfiguration().get('blockchain.ext.directory');
const homeExtDir: string = await UserInputUtil.getDirPath(extDir);
const homeExtDir: string = UserInputUtil.getDirPath(extDir);
const runtimePath: string = path.join(homeExtDir, this.name);
// TODO: hardcoded name
const walletPath: string = path.join(homeExtDir, 'local_wallet');
Expand Down
2 changes: 1 addition & 1 deletion client/src/fabric/FabricWalletGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class FabricWalletGenerator implements IFabricWalletGenerator {
public async createLocalWallet(walletName: string): Promise<FabricWallet> {

const extDir: string = vscode.workspace.getConfiguration().get('blockchain.ext.directory');
const homeExtDir: string = await UserInputUtil.getDirPath(extDir);
const homeExtDir: string = UserInputUtil.getDirPath(extDir);
const walletPath: string = path.join(homeExtDir, walletName);
const walletExists: boolean = await fs.pathExists(walletPath);

Expand Down
2 changes: 1 addition & 1 deletion client/src/packages/PackageRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class PackageRegistry {
// Determine the directory that will contain the packages and ensure it exists.
const extDir: string = vscode.workspace.getConfiguration().get('blockchain.ext.directory');
const pkgDir: string = path.join(extDir, 'packages');
const resolvedPkgDir: string = await UserInputUtil.getDirPath(pkgDir);
const resolvedPkgDir: string = UserInputUtil.getDirPath(pkgDir);
await fs.ensureDir(resolvedPkgDir);

// Read the list of files and process them.
Expand Down
2 changes: 1 addition & 1 deletion client/test/commands/deleteGatewayCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('DeleteGatewayCommand', () => {
data: FabricGatewayRegistry.instance().get('myGatewayC')
});

const getDirPathStub: sinon.SinonStub = mySandBox.stub(UserInputUtil, 'getDirPath').resolves('fabric-vscode');
const getDirPathStub: sinon.SinonStub = mySandBox.stub(UserInputUtil, 'getDirPath').returns('fabric-vscode');
const fsRemoveStub: sinon.SinonStub = mySandBox.stub(fs, 'remove').resolves();

await vscode.commands.executeCommand(ExtensionCommands.DELETE_GATEWAY);
Expand Down
8 changes: 4 additions & 4 deletions client/test/commands/userInputUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,15 +632,15 @@ describe('userInputUtil', () => {
});

describe('getDirPath', () => {
it('should replace ~ with the users home directory', async () => {
it('should replace ~ with the users home directory', () => {
const packageDirOriginal: string = '~/smartContractDir';
const packageDirNew: string = await UserInputUtil.getDirPath(packageDirOriginal);
const packageDirNew: string = UserInputUtil.getDirPath(packageDirOriginal);
packageDirNew.should.not.contain('~');
});

it('should not replace if not ~', async () => {
it('should not replace if not ~', () => {
const packageDirOriginal: string = '/banana/smartContractDir';
const packageDirNew: string = await UserInputUtil.getDirPath(packageDirOriginal);
const packageDirNew: string = UserInputUtil.getDirPath(packageDirOriginal);
packageDirNew.should.equal(packageDirOriginal);
});
});
Expand Down
14 changes: 7 additions & 7 deletions client/test/fabric/FabricGatewayHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('FabricGatewayHelper', () => {
});

it('should copy a connection profile and do nothing if TLS certs are inline', async () => {
getDirPathStub.resolves('fabric-vscode');
getDirPathStub.returns('fabric-vscode');
pathExistsStub.resolves(true);
readFileStub.onFirstCall().resolves(tlsPemJson);
writeFileStub.resolves();
Expand All @@ -94,7 +94,7 @@ describe('FabricGatewayHelper', () => {
});

it('should copy a connection profile and ensure the destination directory exists', async () => {
getDirPathStub.resolves('fabric-vscode');
getDirPathStub.returns('fabric-vscode');
pathExistsStub.resolves(false);
ensureDirStub.resolves();
readFileStub.onFirstCall().resolves(tlsPemJson);
Expand All @@ -112,7 +112,7 @@ describe('FabricGatewayHelper', () => {
});

it('should copy a connection profile and change absolute paths', async () => {
getDirPathStub.resolves('fabric-vscode');
getDirPathStub.returns('fabric-vscode');
pathExistsStub.resolves(false);
ensureDirStub.resolves();
readFileStub.onFirstCall().resolves(tlsPathJson);
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('FabricGatewayHelper', () => {
}
};
const stringifiedObject: string = JSON.stringify(connectionProfileObject);
getDirPathStub.resolves('fabric-vscode');
getDirPathStub.returns('fabric-vscode');
pathExistsStub.resolves(false);
ensureDirStub.resolves();
readFileStub.resolves('CERT_HERE');
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('FabricGatewayHelper', () => {
});

it('should copy a connection profile and change relative paths', async () => {
getDirPathStub.resolves('fabric-vscode');
getDirPathStub.returns('fabric-vscode');
pathExistsStub.resolves(false);
ensureDirStub.resolves();
readFileStub.onFirstCall().resolves(tlsPathJson);
Expand All @@ -203,7 +203,7 @@ describe('FabricGatewayHelper', () => {
});

it('should handle any errors thrown', async () => {
getDirPathStub.resolves('fabric-vscode');
getDirPathStub.returns('fabric-vscode');
pathExistsStub.resolves(false);
ensureDirStub.resolves();
readFileStub.resolves('CERT_HERE');
Expand All @@ -224,7 +224,7 @@ describe('FabricGatewayHelper', () => {
});

it('should copy a connection profile and change relative paths for a YAML file', async () => {
getDirPathStub.resolves('fabric-vscode');
getDirPathStub.returns('fabric-vscode');
pathExistsStub.resolves(false);
ensureDirStub.resolves();
readFileStub.onFirstCall().resolves(yamlPathData);
Expand Down
2 changes: 1 addition & 1 deletion client/test/fabric/FabricRuntime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ describe('FabricRuntime', () => {
getVolumeStub.withArgs('fabricvscodelocalfabric_logs').returns(mockLogsVolume);

runtimeDir = path.join(rootPath, '..', 'data');
sandbox.stub(UserInputUtil, 'getDirPath').resolves(runtimeDir);
sandbox.stub(UserInputUtil, 'getDirPath').returns(runtimeDir);
ensureFileStub = sandbox.stub(fs, 'ensureFileSync').resolves();
writeFileStub = sandbox.stub(fs, 'writeFileSync').resolves();
removeStub = sandbox.stub(fs, 'remove').resolves();
Expand Down
2 changes: 1 addition & 1 deletion client/test/fabric/FabricWalletGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('FabricWalletGenerator', () => {
beforeEach(async () => {
mySandBox = sinon.createSandbox();

mySandBox.stub(UserInputUtil, 'getDirPath').resolves(path.join(rootPath, '../../test/data/walletDir'));
mySandBox.stub(UserInputUtil, 'getDirPath').returns(path.join(rootPath, '../../test/data/walletDir'));
ensureDirStub = mySandBox.stub(fs, 'ensureDir').resolves();
pathExistsStub = mySandBox.stub(fs, 'pathExists');
});
Expand Down

0 comments on commit 93a76b7

Please sign in to comment.