diff --git a/src/tfvc/tfvcerror.ts b/src/tfvc/tfvcerror.ts index 3501b9a1d4..3d84b34d4a 100644 --- a/src/tfvc/tfvcerror.ts +++ b/src/tfvc/tfvcerror.ts @@ -43,6 +43,10 @@ export class TfvcError { } public constructor(data: ITfvcErrorData) { + if (!data) { + throw TfvcError.CreateArgumentMissingError("data"); + } + if (data.error) { this.error = data.error; this.message = data.error.message; @@ -59,20 +63,14 @@ export class TfvcError { } public toString(): string { - let result = this.message + " " + - JSON.stringify( - { - exitCode: this.exitCode, - TfvcErrorCode: this.tfvcErrorCode, - gitCommand: this.tfvcCommand, - stdout: this.stdout, - stderr: this.stderr - }, - [], - 2); - + let result = this.message + " Details: " + + `exitCode: ${this.exitCode}, ` + + `errorCode: ${this.tfvcErrorCode}, ` + + `command: ${this.tfvcCommand}, ` + + `stdout: ${this.stdout}, ` + + `stderr: ${this.stderr}`; if (this.error) { - result += (this.error).stack; + result += " Stack: " + (this.error).stack; } return result; diff --git a/test/tfvc/tfvcerror.test.ts b/test/tfvc/tfvcerror.test.ts new file mode 100644 index 0000000000..0b214053d0 --- /dev/null +++ b/test/tfvc/tfvcerror.test.ts @@ -0,0 +1,145 @@ +/*--------------------------------------------------------------------------------------------- +* Copyright (c) Microsoft Corporation. All rights reserved. +* Licensed under the MIT License. See License.txt in the project root for license information. +*--------------------------------------------------------------------------------------------*/ +"use strict"; + +import { assert } from "chai"; + +import { Strings } from "../../src/helpers/strings"; +import { TfvcError, TfvcErrorCodes } from "../../src/tfvc/tfvcerror"; + +describe("Tfvc-Error", function() { + beforeEach(function() { + // + }); + + it("should verify constructor - undefined", function() { + assert.throws(() => new TfvcError(undefined), TfvcError, /Argument is required/); + }); + + it("should verify constructor - empty data", function() { + let error: TfvcError = new TfvcError({ + error: undefined, + exitCode: 0, + message: undefined, + stderr: undefined, + stdout: undefined, + tfvcCommand: undefined, + tfvcErrorCode: undefined + }); + assert.equal(error.error, undefined); + assert.equal(error.exitCode, 0); + assert.equal(error.message, Strings.TfExecFailedError); + assert.equal(error.stderr, undefined); + assert.equal(error.stdout, undefined); + assert.equal(error.tfvcCommand, undefined); + assert.equal(error.tfvcErrorCode, undefined); + }); + + it("should verify constructor - error not empty", function() { + let error: TfvcError = new TfvcError({ + error: { name: "err1", message: "error1 message" }, + exitCode: 0, + message: undefined, + stderr: undefined, + stdout: undefined, + tfvcCommand: undefined, + tfvcErrorCode: undefined + }); + assert.equal(error.error.name, "err1"); + assert.equal(error.error.message, "error1 message"); + assert.equal(error.exitCode, 0); + assert.equal(error.message, "error1 message"); + assert.equal(error.stderr, undefined); + assert.equal(error.stdout, undefined); + assert.equal(error.tfvcCommand, undefined); + assert.equal(error.tfvcErrorCode, undefined); + }); + + it("should verify constructor - error.message over message", function() { + let error: TfvcError = new TfvcError({ + error: { name: "err1", message: "error1 message" }, + exitCode: 0, + message: "other message", + stderr: undefined, + stdout: undefined, + tfvcCommand: undefined, + tfvcErrorCode: undefined + }); + assert.equal(error.error.name, "err1"); + assert.equal(error.error.message, "error1 message"); + assert.equal(error.exitCode, 0); + assert.equal(error.message, "error1 message"); + assert.equal(error.stderr, undefined); + assert.equal(error.stdout, undefined); + assert.equal(error.tfvcCommand, undefined); + assert.equal(error.tfvcErrorCode, undefined); + }); + + it("should verify constructor - no error", function() { + let error: TfvcError = new TfvcError({ + error: undefined, + exitCode: 100, + message: "other message", + stderr: "standard error", + stdout: "standard output", + tfvcCommand: "command1", + tfvcErrorCode: TfvcErrorCodes.TfvcLocationMissing + }); + assert.equal(error.error, undefined); + assert.equal(error.exitCode, 100); + assert.equal(error.message, "other message"); + assert.equal(error.stderr, "standard error"); + assert.equal(error.stdout, "standard output"); + assert.equal(error.tfvcCommand, "command1"); + assert.equal(error.tfvcErrorCode, TfvcErrorCodes.TfvcLocationMissing); + }); + + it("should verify CreateArgumentMissingError", function() { + let error: TfvcError = TfvcError.CreateArgumentMissingError("arg1"); + assert.equal(error.error, undefined); + assert.equal(error.exitCode, undefined); + assert.equal(error.message, "Argument is required: arg1"); + assert.equal(error.stderr, undefined); + assert.equal(error.stdout, undefined); + assert.equal(error.tfvcCommand, undefined); + assert.equal(error.tfvcErrorCode, TfvcErrorCodes.MissingArgument); + }); + + it("should verify CreateInvalidStateError", function() { + let error: TfvcError = TfvcError.CreateInvalidStateError(); + assert.equal(error.error, undefined); + assert.equal(error.exitCode, undefined); + assert.equal(error.message, "The TFVC SCMProvider is in an invalid state for this action."); + assert.equal(error.stderr, undefined); + assert.equal(error.stdout, undefined); + assert.equal(error.tfvcCommand, undefined); + assert.equal(error.tfvcErrorCode, TfvcErrorCodes.TfvcInInvalidState); + }); + + it("should verify CreateUnknownError", function() { + let error: TfvcError = TfvcError.CreateUnknownError({ name: "err1", message: "error1 message" }); + assert.equal(error.error.name, "err1"); + assert.equal(error.error.message, "error1 message"); + assert.equal(error.exitCode, undefined); + assert.equal(error.message, "error1 message"); + assert.equal(error.stderr, undefined); + assert.equal(error.stdout, undefined); + assert.equal(error.tfvcCommand, undefined); + assert.equal(error.tfvcErrorCode, TfvcErrorCodes.UnknownError); + }); + + it("should verify toString", function() { + let error: TfvcError = new TfvcError({ + error: { name: "err1", message: "error1 message", stack: "here; then there" }, + exitCode: 11, + message: undefined, + stderr: "standard error", + stdout: "standard output", + tfvcCommand: "command1", + tfvcErrorCode: TfvcErrorCodes.TfvcMinVersionWarning + }); + assert.equal(error.toString(), "error1 message Details: exitCode: 11, errorCode: TfvcMinVersionWarning, command: command1, stdout: standard output, stderr: standard error Stack: here; then there"); + }); +}); diff --git a/test/tfvc/tfvcversion.test.ts b/test/tfvc/tfvcversion.test.ts index 254fb4f226..652da0ec08 100644 --- a/test/tfvc/tfvcversion.test.ts +++ b/test/tfvc/tfvcversion.test.ts @@ -16,31 +16,64 @@ describe("Tfvc-Version", function() { it("should verify constructor", function() { let version: TfvcVersion = new TfvcVersion(12, 11, 10, ""); assert.equal(version.ToString(), "12.11.10"); + assert.equal(version.Major, 12); + assert.equal(version.Minor, 11); + assert.equal(version.Revision, 10); + assert.equal(version.Build, ""); }); it("should verify constructor - with build", function() { let version: TfvcVersion = new TfvcVersion(12, 11, 10, "buildpart"); assert.equal(version.ToString(), "12.11.10.buildpart"); + assert.equal(version.Major, 12); + assert.equal(version.Minor, 11); + assert.equal(version.Revision, 10); + assert.equal(version.Build, "buildpart"); }); it("should verify constructor - with dotted build", function() { let version: TfvcVersion = new TfvcVersion(12, 11, 10, "build.part."); assert.equal(version.ToString(), "12.11.10.build.part."); + assert.equal(version.Major, 12); + assert.equal(version.Minor, 11); + assert.equal(version.Revision, 10); + assert.equal(version.Build, "build.part."); }); it("should verify FromString", function() { let version: TfvcVersion = TfvcVersion.FromString("12.11.10.build.part."); assert.equal(version.ToString(), "12.11.10.build.part."); + assert.equal(version.Major, 12); + assert.equal(version.Minor, 11); + assert.equal(version.Revision, 10); + assert.equal(version.Build, "build.part."); }); it("should verify FromString - missing build", function() { let version: TfvcVersion = TfvcVersion.FromString("12.11.10"); assert.equal(version.ToString(), "12.11.10"); + assert.equal(version.Major, 12); + assert.equal(version.Minor, 11); + assert.equal(version.Revision, 10); + assert.equal(version.Build, ""); }); it("should verify FromString - missing revision", function() { let version: TfvcVersion = TfvcVersion.FromString("12.11"); assert.equal(version.ToString(), "12.11.0"); + assert.equal(version.Major, 12); + assert.equal(version.Minor, 11); + assert.equal(version.Revision, 0); + assert.equal(version.Build, ""); + }); + + it("should verify FromString - undefined", function() { + let version: TfvcVersion = TfvcVersion.FromString(undefined); + assert.equal(version.ToString(), "0.0.0"); + assert.equal(version.Major, 0); + assert.equal(version.Minor, 0); + assert.equal(version.Revision, 0); + assert.equal(version.Build, ""); }); it("should verify Compare", function() { @@ -50,6 +83,20 @@ describe("Tfvc-Version", function() { assert.isTrue(TfvcVersion.Compare(version2, version1) > 0); }); + it("should verify Compare - major difference", function() { + let version1: TfvcVersion = TfvcVersion.FromString("12.11"); + let version2: TfvcVersion = TfvcVersion.FromString("13.1.1"); + assert.isTrue(TfvcVersion.Compare(version1, version2) < 0); + assert.isTrue(TfvcVersion.Compare(version2, version1) > 0); + }); + + it("should verify Compare - minor difference", function() { + let version1: TfvcVersion = TfvcVersion.FromString("12.11"); + let version2: TfvcVersion = TfvcVersion.FromString("12.13.1"); + assert.isTrue(TfvcVersion.Compare(version1, version2) < 0); + assert.isTrue(TfvcVersion.Compare(version2, version1) > 0); + }); + it("should verify Compare - equals", function() { let version1: TfvcVersion = TfvcVersion.FromString("12.11.10"); let version2: TfvcVersion = TfvcVersion.FromString("12.11.10");