Skip to content

Commit

Permalink
Merge pull request #1204 from arturcic/feature/improve-testability
Browse files Browse the repository at this point in the history
Improve testability
  • Loading branch information
arturcic authored Jul 25, 2024
2 parents f32f060 + 14c9265 commit 767faa2
Show file tree
Hide file tree
Showing 30 changed files with 332 additions and 305 deletions.
15 changes: 8 additions & 7 deletions .azure/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ jobs:
npx tfx extension create --root ./dist/azure --manifest-js ./dist/azure/manifest.config.cjs
Expand-Archive -Path gittools.gittools-0.0.1.vsix -DestinationPath dist/vsix
displayName: 'Build code'
- pwsh: |
npm run test:ci
displayName: 'Run tests'
- task: PublishTestResults@2
inputs:
testResultsFormat: 'JUnit'
testResultsFiles: 'junit-report.xml'
displayName: 'Test Summary'
- pwsh: |
# set the inputs for the 'gitversion/setup' action
$env:INPUT_VERSIONSPEC = "$(INPUT_VERSIONSPEC)"
Expand All @@ -76,13 +84,6 @@ jobs:
name: version
displayName: gitversion/execute
workingDirectory: dist/vsix
- pwsh: |
npm run test:ci
displayName: 'Run tests'
- task: PublishTestResults@2
inputs:
testResultsFormat: 'JUnit'
testResultsFiles: 'junit-report.xml'
- pwsh: |
echo "Major (major) : $(major)"
echo "Major (GitVersion_Major) : $(GitVersion_Major)"
Expand Down
8 changes: 7 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@
"never"
],
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/unbound-method": "error"
"@typescript-eslint/unbound-method": "error",
"vitest/expect-expect": [
"error",
{
"assertFunctionNames": ["expect", "expectValidSettings"]
}
]
},
"ignorePatterns": [
"packages/glob/__tests__/_temp/**/",
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ jobs:
npm run build:agent:github
npm run build:agent:azure
name: Build code
- run: |
npm run test:ci
name: Run tests
- name: Test Summary
uses: test-summary/action@v2.4
with:
paths: junit-report.xml
- name: gitversion/setup
uses: ./gitversion/setup
with:
Expand All @@ -56,13 +63,6 @@ jobs:
disableNormalization: false
overrideConfig: |
update-build-number=false
- run: |
npm run test:ci
name: Run tests
- name: Test Summary
uses: test-summary/action@v2.4
with:
paths: junit-report.xml
- run: |
echo "Major (env.major) : ${{ env.major }}"
echo "Major (env.GitVersion_Major) : ${{ env.GitVersion_Major }}"
Expand Down
4 changes: 2 additions & 2 deletions dist/azure/gitreleasemanager/setup/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"label": "The version spec",
"defaultValue": "",
"required": true,
"helpMarkDown": "Required version in the form of 0.17.x or exact version like 0.17.0"
"helpMarkDown": "Required version in the form of 0.18.x or exact version like 0.18.0"
},
{
"name": "includePrerelease",
Expand All @@ -55,4 +55,4 @@
"helpMarkDown": "Prefer to download the latest version matching the versionSpec, even if there is a local cached version."
}
]
}
}
63 changes: 43 additions & 20 deletions dist/tools/libs/gitreleasemanager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -282,96 +282,119 @@ class Runner {
this.buildAgent.info(`Set ${pathVariable} to ${toolPath}`);
this.buildAgent.setVariable(pathVariable, toolPath);
this.buildAgent.setSucceeded("GitReleaseManager installed successfully", true);
return 0;
return {
code: 0
};
} catch (error) {
if (error instanceof Error) {
this.buildAgent.setFailed(error.message, true);
}
return -1;
return {
code: -1,
error
};
}
}
async create() {
try {
this.disableTelemetry();
this.buildAgent.debug("Creating release");
await this.gitReleaseManagerTool.create();
const result = await this.gitReleaseManagerTool.create();
this.buildAgent.setSucceeded("GitReleaseManager created release successfully", true);
return 0;
return result;
} catch (error) {
if (error instanceof Error) {
this.buildAgent.setFailed(error.message, true);
}
return -1;
return {
code: -1,
error
};
}
}
async discard() {
try {
this.disableTelemetry();
this.buildAgent.debug("Discarding release");
await this.gitReleaseManagerTool.discard();
const result = await this.gitReleaseManagerTool.discard();
this.buildAgent.setSucceeded("GitReleaseManager discarded release successfully", true);
return 0;
return result;
} catch (error) {
if (error instanceof Error) {
this.buildAgent.setFailed(error.message, true);
}
return -1;
return {
code: -1,
error
};
}
}
async close() {
try {
this.disableTelemetry();
this.buildAgent.debug("Closing release");
await this.gitReleaseManagerTool.close();
const result = await this.gitReleaseManagerTool.close();
this.buildAgent.setSucceeded("GitReleaseManager closed release successfully", true);
return 0;
return result;
} catch (error) {
if (error instanceof Error) {
this.buildAgent.setFailed(error.message, true);
}
return -1;
return {
code: -1,
error
};
}
}
async open() {
try {
this.disableTelemetry();
this.buildAgent.debug("Opening release");
await this.gitReleaseManagerTool.open();
const result = await this.gitReleaseManagerTool.open();
this.buildAgent.setSucceeded("GitReleaseManager opened release successfully", true);
return 0;
return result;
} catch (error) {
if (error instanceof Error) {
this.buildAgent.setFailed(error.message, true);
}
return -1;
return {
code: -1,
error
};
}
}
async publish() {
try {
this.disableTelemetry();
this.buildAgent.debug("Publishing release");
await this.gitReleaseManagerTool.publish();
const result = await this.gitReleaseManagerTool.publish();
this.buildAgent.setSucceeded("GitReleaseManager published release successfully", true);
return 0;
return result;
} catch (error) {
if (error instanceof Error) {
this.buildAgent.setFailed(error.message, true);
}
return -1;
return {
code: -1,
error
};
}
}
async addAsset() {
try {
this.disableTelemetry();
this.buildAgent.debug("Adding asset to release");
await this.gitReleaseManagerTool.addAsset();
const result = await this.gitReleaseManagerTool.addAsset();
this.buildAgent.setSucceeded("GitReleaseManager added assets to release successfully", true);
return 0;
return result;
} catch (error) {
if (error instanceof Error) {
this.buildAgent.setFailed(error.message, true);
}
return -1;
return {
code: -1,
error
};
}
}
disableTelemetry() {
Expand Down
2 changes: 1 addition & 1 deletion dist/tools/libs/gitreleasemanager.mjs.map

Large diffs are not rendered by default.

42 changes: 26 additions & 16 deletions dist/tools/libs/gitversion.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { S as SettingsProvider, D as DotnetTool, k as keysFn } from './tools.mjs';
import { S as SettingsProvider, D as DotnetTool, k as keysOf } from './tools.mjs';
import 'node:crypto';
import 'node:fs/promises';
import 'node:os';
Expand All @@ -20,7 +20,7 @@ var ExecuteFields = /* @__PURE__ */ ((ExecuteFields2) => {
})(ExecuteFields || {});

class GitVersionSettingsProvider extends SettingsProvider {
getGitVersionSettings() {
getGitVersionExecuteSettings() {
const targetPath = this.buildAgent.getInput(ExecuteFields.targetPath);
const disableCache = this.buildAgent.getBooleanInput(ExecuteFields.disableCache);
const disableNormalization = this.buildAgent.getBooleanInput(ExecuteFields.disableNormalization);
Expand Down Expand Up @@ -62,17 +62,16 @@ class GitVersionTool extends DotnetTool {
get settingsProvider() {
return new GitVersionSettingsProvider(this.buildAgent);
}
async run() {
const settings = this.settingsProvider.getGitVersionSettings();
async executeJson() {
const settings = this.settingsProvider.getGitVersionExecuteSettings();
const workDir = await this.getRepoDir(settings);
await this.checkShallowClone(settings, workDir);
const args = await this.getArguments(workDir, settings);
const args = await this.getExecuteArguments(workDir, settings);
await this.setDotnetRoot();
return await this.executeTool(args);
}
writeGitVersionToAgent(output) {
const keys = keysFn(output);
for (const property of keys) {
for (const property of keysOf(output)) {
const name = this.toCamelCase(property);
try {
let value = output[property]?.toString();
Expand All @@ -91,7 +90,7 @@ class GitVersionTool extends DotnetTool {
async getRepoDir(settings) {
return await super.getRepoPath(settings.targetPath);
}
async getArguments(workDir, options) {
async getExecuteArguments(workDir, options) {
let args = [workDir, "/output", "json", "/output", "buildserver"];
const {
useConfigFile,
Expand Down Expand Up @@ -230,22 +229,27 @@ class Runner {
this.buildAgent.info(`Set ${pathVariable} to ${toolPath}`);
this.buildAgent.setVariable(pathVariable, toolPath);
this.buildAgent.setSucceeded("GitVersion installed successfully", true);
return 0;
return {
code: 0
};
} catch (error) {
if (error instanceof Error) {
this.buildAgent.setFailed(error.message, true);
}
return -1;
return {
code: -1,
error
};
}
}
async execute() {
try {
this.disableTelemetry();
this.buildAgent.info("Executing GitVersion");
const result = await this.gitVersionTool.run();
const result = await this.gitVersionTool.executeJson();
if (result.code === 0) {
this.buildAgent.info("GitVersion executed successfully");
const { stdout } = result;
const stdout = result.stdout;
this.buildAgent.info("GitVersion output:");
this.buildAgent.info("-------------------");
this.buildAgent.info(stdout);
Expand All @@ -254,27 +258,33 @@ class Runner {
if (stdout.lastIndexOf("{") === -1 || stdout.lastIndexOf("}") === -1) {
this.buildAgent.debug("GitVersion output is not valid JSON");
this.buildAgent.setFailed("GitVersion output is not valid JSON", true);
return -1;
return {
code: -1,
error: new Error("GitVersion output is not valid JSON")
};
} else {
const jsonOutput = stdout.substring(stdout.lastIndexOf("{"), stdout.lastIndexOf("}") + 1);
const gitVersionOutput = JSON.parse(jsonOutput);
this.gitVersionTool.writeGitVersionToAgent(gitVersionOutput);
this.buildAgent.setSucceeded("GitVersion executed successfully", true);
return 0;
return result;
}
} else {
this.buildAgent.debug("GitVersion failed");
const error = result.error;
if (error instanceof Error) {
this.buildAgent.setFailed(error.message, true);
}
return -1;
return result;
}
} catch (error) {
if (error instanceof Error) {
this.buildAgent.setFailed(error.message, true);
}
return -1;
return {
code: -1,
error
};
}
}
disableTelemetry() {
Expand Down
2 changes: 1 addition & 1 deletion dist/tools/libs/gitversion.mjs.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/tools/libs/tools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class SettingsProvider {
}
}

const keysFn = Object.keys;
const keysOf = Object.keys;

export { DotnetTool as D, SettingsProvider as S, keysFn as k };
export { DotnetTool as D, SettingsProvider as S, keysOf as k };
//# sourceMappingURL=tools.mjs.map
2 changes: 1 addition & 1 deletion dist/tools/libs/tools.mjs.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/examples/azure/gitversion/execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ steps:
<details>
<summary>Calculate the version for the build using a config file with the default name <b>GitVersion.yml</b>.</summary>
```yaml
steps:
# gitversion/setup@2.0.1 task omitted for brevity.

- task: gitversion/execute@2.0.1
- task: gitversion/execute@2.0.1
displayName: Determine Version
inputs:
useConfigFile: true
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/azure/gitversion/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Note: You need to run the Setup step before the Execute step, otherwise the Exec

## Setup GitVersion (gitversion/setup)

- [Usage examples](setup.md)
- [Setup](setup.md)

## Execute GitVersion (gitversion/execute)

- [Usage examples](execute.md)
- [Execute](execute.md)
4 changes: 2 additions & 2 deletions docs/examples/github/gitversion/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Note: You need to run the Setup step before the Execute step, otherwise the Exec

## Setup GitVersion (gitversion/setup)

- [Usage examples](setup.md)
- [Setup](setup.md)

## Execute GitVersion (gitversion/execute)

- [Usage examples](execute.md)
- [Execute](execute.md)
2 changes: 1 addition & 1 deletion gitreleasemanager/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ runs:
main: main.mjs
inputs:
versionSpec:
description: Required version in the form of 0.17.x or exact version like 0.17.0
description: Required version in the form of 0.18.x or exact version like 0.18.0
required: true
default: ''
includePrerelease:
Expand Down
Loading

0 comments on commit 767faa2

Please sign in to comment.