Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Additional unit tests #125

Merged
merged 2 commits into from
Feb 23, 2017
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
12 changes: 8 additions & 4 deletions src/tfvc/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ export class Add implements ITfvcCommand<string[]> {
* file2.java
*/
public async ParseOutput(executionResult: IExecutionResult): Promise<string[]> {
// Throw if any errors are found in stderr or if exitcode is not 0
CommandHelper.ProcessErrors(this.GetArguments().GetCommand(), executionResult);
// Any exit code other than 0 or 1 means that something went wrong, so simply throw the error
if (executionResult.exitCode !== 0 && executionResult.exitCode !== 1) {
CommandHelper.ProcessErrors(this.GetArguments().GetCommand(), executionResult);
}

let lines: string[] = CommandHelper.SplitIntoLines(executionResult.stdout, false, true /*filterEmptyLines*/);

//Remove any lines indicating that there were no files to add (e.g., calling add on files that don't exist)
lines = lines.filter(e => !e.startsWith("No arguments matched any files to add."));
lines = lines.filter(e => !e.startsWith("No arguments matched any files to add.")); //CLC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could combine the two filter lines into one. But that may make it less readable.

//Ex. /usr/alias/repos/Tfvc.L2VSCodeExtension.RC/file-does-not-exist.md: No file matches.
lines = lines.filter(e => !e.endsWith(" No file matches.")); //tf.exe

let filesAdded: string[] = [];
let path: string = "";
Expand All @@ -72,7 +76,7 @@ export class Add implements ITfvcCommand<string[]> {
}

public async ParseExeOutput(executionResult: IExecutionResult): Promise<string[]> {
return this.ParseOutput(executionResult);
return await this.ParseOutput(executionResult);
}

private getFileFromLine(line: string): string {
Expand Down
4 changes: 2 additions & 2 deletions src/tfvc/commands/checkin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class Checkin implements ITfvcCommand<string> {
return this.GetOptions();
}

public ParseExeOutput(executionResult: IExecutionResult): Promise<string> {
return this.ParseOutput(executionResult);
public async ParseExeOutput(executionResult: IExecutionResult): Promise<string> {
return await this.ParseOutput(executionResult);
}
}
4 changes: 2 additions & 2 deletions src/tfvc/commands/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export class Delete implements ITfvcCommand<string[]> {
return this.GetOptions();
}

public ParseExeOutput(executionResult: IExecutionResult): Promise<string[]> {
return this.ParseOutput(executionResult);
public async ParseExeOutput(executionResult: IExecutionResult): Promise<string[]> {
return await this.ParseOutput(executionResult);
}

private getFileFromLine(line: string): string {
Expand Down
8 changes: 4 additions & 4 deletions src/tfvc/commands/getfilecontent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class GetFileContent implements ITfvcCommand<string> {
private _versionSpec: string;
private _ignoreFileNotFound: boolean;

public constructor(serverContext: TeamServerContext, localPath: string, versionSpec: string, ignoreFileNotFound: boolean) {
public constructor(serverContext: TeamServerContext, localPath: string, versionSpec?: string, ignoreFileNotFound?: boolean) {
CommandHelper.RequireStringArgument(localPath, "localPath");

this._serverContext = serverContext;
Expand All @@ -35,7 +35,7 @@ export class GetFileContent implements ITfvcCommand<string> {
let builder: ArgumentBuilder = new ArgumentBuilder("print", this._serverContext)
.Add(this._localPath);
if (this._versionSpec) {
builder.AddSwitchWithValue("version", this._versionSpec.toString(), false);
builder.AddSwitchWithValue("version", this._versionSpec, false);
}
return builder;
}
Expand Down Expand Up @@ -66,7 +66,7 @@ export class GetFileContent implements ITfvcCommand<string> {
let builder: ArgumentBuilder = new ArgumentBuilder("view", this._serverContext)
.Add(this._localPath);
if (this._versionSpec) {
builder.AddSwitchWithValue("version", this._versionSpec.toString(), false);
builder.AddSwitchWithValue("version", this._versionSpec, false);
}
return builder;
}
Expand All @@ -76,6 +76,6 @@ export class GetFileContent implements ITfvcCommand<string> {
}

public async ParseExeOutput(executionResult: IExecutionResult): Promise<string> {
return this.ParseOutput(executionResult);
return await this.ParseOutput(executionResult);
}
}
20 changes: 8 additions & 12 deletions src/tfvc/commands/getversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ export class GetVersion implements ITfvcCommand<string> {
}

public async ParseOutput(executionResult: IExecutionResult): Promise<string> {
// Throw if any errors are found in stderr or if exitcode is not 0
CommandHelper.ProcessErrors(this.GetArguments().GetCommand(), executionResult);

const lines: string[] = CommandHelper.SplitIntoLines(executionResult.stdout);
// Find just the version number and return it. Ex. Team Explorer Everywhere Command Line Client (Version 14.0.3.201603291047)
if (lines && lines.length > 0) {
return lines[0].replace(/(.*\(version )([\.\d]*)(\).*)/i, "$2");
} else {
return "";
}
//Ex. Team Explorer Everywhere Command Line Client (Version 14.0.3.201603291047)
return await this.getVersion(executionResult, /(.*\(version )([\.\d]*)(\).*)/i);
}

public GetExeArguments(): IArgumentProvider {
Expand All @@ -44,15 +36,19 @@ export class GetVersion implements ITfvcCommand<string> {
return this.GetOptions();
}

//TODO: Refactor this with ParseOutput (pass in just the line and the regex?)
public async ParseExeOutput(executionResult: IExecutionResult): Promise<string> {
//Ex. Microsoft (R) TF - Team Foundation Version Control Tool, Version 14.102.25619.0
return await this.getVersion(executionResult, /(.*version )([\.\d]*)(.*)/i);
}

private async getVersion(executionResult: IExecutionResult, expression: RegExp): Promise<string> {
// Throw if any errors are found in stderr or if exitcode is not 0
CommandHelper.ProcessErrors(this.GetArguments().GetCommand(), executionResult);

const lines: string[] = CommandHelper.SplitIntoLines(executionResult.stdout);
// Find just the version number and return it. Ex. Microsoft (R) TF - Team Foundation Version Control Tool, Version 14.102.25619.0
if (lines && lines.length > 0) {
let value: string = lines[0].replace(/(.*version )([\.\d]*)(.*)/i, "$2");
let value: string = lines[0].replace(expression, "$2");
return value;
} else {
return "";
Expand Down
144 changes: 140 additions & 4 deletions test/tfvc/commands/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,40 @@ describe("Tfvc-AddCommand", function() {
assert.deepEqual(cmd.GetOptions(), {});
});

it("should verify GetExeOptions", function() {
let localPaths: string[] = ["/usr/alias/repos/Tfvc.L2VSCodeExtension.RC/README.md"];
let cmd: Add = new Add(undefined, localPaths);
assert.deepEqual(cmd.GetExeOptions(), {});
});

it("should verify arguments", function() {
let localPaths: string[] = ["/usr/alias/repos/Tfvc.L2VSCodeExtension.RC/README.md"];
let cmd: Add = new Add(undefined, localPaths);

assert.equal(cmd.GetArguments().GetArgumentsForDisplay(), "add -noprompt " + localPaths[0]);
});

it("should verify Exe arguments", function() {
let localPaths: string[] = ["/usr/alias/repos/Tfvc.L2VSCodeExtension.RC/README.md"];
let cmd: Add = new Add(undefined, localPaths);

assert.equal(cmd.GetExeArguments().GetArgumentsForDisplay(), "add -noprompt " + localPaths[0]);
});

it("should verify arguments with context", function() {
let localPaths: string[] = ["/usr/alias/repos/Tfvc.L2VSCodeExtension.RC/README.md"];
let cmd: Add = new Add(context, localPaths);

assert.equal(cmd.GetArguments().GetArgumentsForDisplay(), "add -noprompt -collection:" + collectionUrl + " ******** " + localPaths[0]);
});

it("should verify Exe arguments with context", function() {
let localPaths: string[] = ["/usr/alias/repos/Tfvc.L2VSCodeExtension.RC/README.md"];
let cmd: Add = new Add(context, localPaths);

assert.equal(cmd.GetExeArguments().GetArgumentsForDisplay(), "add -noprompt ******** " + localPaths[0]);
});

it("should verify parse output - no files to add", async function() {
let localPaths: string[] = ["/usr/alias/repos/Tfvc.L2VSCodeExtension.RC/file-does-not-exist.md"];
let cmd: Add = new Add(undefined, localPaths);
Expand Down Expand Up @@ -173,8 +193,8 @@ describe("Tfvc-AddCommand", function() {
} catch (err) {
assert.equal(err.exitCode, 42);
assert.equal(err.tfvcCommand, "add");
assert.equal(err.message.indexOf(Strings.TfExecFailedError), 0);
assert.equal(err.stdout.indexOf("Something bad this way comes."), 0);
assert.isTrue(err.message.startsWith(Strings.TfExecFailedError));
assert.isTrue(err.stdout.startsWith("Something bad this way comes."));
}
});

Expand All @@ -193,9 +213,125 @@ describe("Tfvc-AddCommand", function() {
} catch (err) {
assert.equal(err.exitCode, 42);
assert.equal(err.tfvcCommand, "add");
assert.equal(err.message.indexOf(Strings.TfExecFailedError), 0);
assert.equal(err.stderr.indexOf("Something bad this way comes."), 0);
assert.isTrue(err.message.startsWith(Strings.TfExecFailedError));
assert.isTrue(err.stderr.startsWith("Something bad this way comes."));
}
});

/// Verify ParseExeOutput values (for tf.exe)
it("should verify parse Exe output - no files to add", async function() {
let localPaths: string[] = ["/usr/alias/repos/Tfvc.L2VSCodeExtension.RC/file-does-not-exist.md"];
let cmd: Add = new Add(undefined, localPaths);
//This return value is different for tf.exe than the CLC
let executionResult: IExecutionResult = {
exitCode: 1,
stdout: "/usr/alias/repos/Tfvc.L2VSCodeExtension.RC/file-does-not-exist.md: No file matches.",
stderr: undefined
};

let filesAdded: string[] = await cmd.ParseExeOutput(executionResult);
assert.equal(filesAdded.length, 0);
});

it("should verify parse Exe output - single empty folder - no errors", async function() {
let localPaths: string[] = ["empty-folder"];
let cmd: Add = new Add(undefined, localPaths);
//This return value is different for tf.exe than the CLC
let executionResult: IExecutionResult = {
exitCode: 0,
stdout: "empty-folder\n",
stderr: undefined
};

let filesAdded: string[] = await cmd.ParseExeOutput(executionResult);
assert.equal(filesAdded.length, 1);
//In this case, the EXE returns (this differs from the CLC):
//empty-folder
assert.equal(filesAdded[0], localPaths[0]);
});

it("should verify parse Exe output - single folder+file - no errors", async function() {
let localPaths: string[] = [path.join("folder1", "file1.txt")];
let cmd: Add = new Add(undefined, localPaths);
let executionResult: IExecutionResult = {
exitCode: 0,
stdout: "folder1:\n" +
"file1.txt\n",
stderr: undefined
};

let filesAdded: string[] = await cmd.ParseExeOutput(executionResult);
assert.equal(filesAdded.length, 1);
assert.equal(filesAdded[0], localPaths[0]);
});

it("should verify parse Exe output - single subfolder+file - no errors", async function() {
let localPaths: string[] = [path.join("folder1", "folder2", "file2.txt")];
let cmd: Add = new Add(undefined, localPaths);
let executionResult: IExecutionResult = {
exitCode: 0,
stdout: path.join("folder1", "folder2") + ":\n" +
"file2.txt\n",
stderr: undefined
};

let filesAdded: string[] = await cmd.ParseExeOutput(executionResult);
assert.equal(filesAdded.length, 1);
assert.equal(filesAdded[0], localPaths[0]);
});

it("should verify parse Exe output - single folder+file - spaces - no errors", async function() {
let localPaths: string[] = [path.join("fold er1", "file1.txt")];
let cmd: Add = new Add(undefined, localPaths);
let executionResult: IExecutionResult = {
exitCode: 0,
stdout: "fold er1:\n" +
"file1.txt\n",
stderr: undefined
};

let filesAdded: string[] = await cmd.ParseExeOutput(executionResult);
assert.equal(filesAdded.length, 1);
assert.equal(filesAdded[0], localPaths[0]);
});

it("should verify parse Exe output - error exit code, stdout", async function() {
let noChangesPaths: string[] = [path.join("folder1", "file1.txt"), path.join("folder2", "file2.txt")];
let localPaths: string[] = noChangesPaths;
let cmd: Add = new Add(undefined, localPaths);
let executionResult: IExecutionResult = {
exitCode: 42,
stdout: "Something bad this way comes.",
stderr: undefined
};

try {
await cmd.ParseExeOutput(executionResult);
} catch (err) {
assert.equal(err.exitCode, 42);
assert.equal(err.tfvcCommand, "add");
assert.isTrue(err.message.startsWith(Strings.TfExecFailedError));
assert.isTrue(err.stdout.startsWith("Something bad this way comes."));
}
});

it("should verify parse Exe output - error exit code, stderr", async function() {
let noChangesPaths: string[] = [path.join("folder1", "file1.txt"), path.join("folder2", "file2.txt")];
let localPaths: string[] = noChangesPaths;
let cmd: Add = new Add(undefined, localPaths);
let executionResult: IExecutionResult = {
exitCode: 42,
stdout: undefined,
stderr: "Something bad this way comes."
};

try {
await cmd.ParseExeOutput(executionResult);
} catch (err) {
assert.equal(err.exitCode, 42);
assert.equal(err.tfvcCommand, "add");
assert.isTrue(err.message.startsWith(Strings.TfExecFailedError));
assert.isTrue(err.stderr.startsWith("Something bad this way comes."));
}
});
});
Loading