Skip to content

Commit

Permalink
Avoids installing the Cake.Tool more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
ecampidoglio committed Oct 28, 2019
1 parent 67ad57e commit e24c3e5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 0 deletions.
18 changes: 18 additions & 0 deletions __tests__/dotnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,21 @@ describe('When failing to install the a tool locally', () => {
await expect(DotNet.installLocalTool('The.Tool')).rejects.toThrowError('-99');
});
});

describe('When installing the Cake Tool locally to a directory where it already exists', () => {
const directoryWithCakeTool = new ToolsDirectory(targetDirectory);
const fakeExec = exec as jest.MockedFunction<typeof exec>;

beforeAll(() => {
directoryWithCakeTool.containsFile = jest.fn().mockImplementation(() => {
return true;
});
});

test('it should not attempt to install the Cake.Tool in the same directory', () => {
DotNet.installLocalCakeTool(directoryWithCakeTool);
expect(fakeExec).not.toBeCalledWith(
'dotnet tool install',
['--tool-path', directoryWithCakeTool.path, 'Cake.Tool']);
});
});
7 changes: 7 additions & 0 deletions lib/dotnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const exec_1 = require("@actions/exec");
const toolsDirectory_1 = require("./toolsDirectory");
const platform_1 = require("./platform");
const dotnetToolInstall = 'dotnet tool install';
const dotnetCake = 'dotnet-cake';
class DotNet {
static disableTelemetry() {
core.exportVariable('DOTNET_CLI_TELEMETRY_OPTOUT', '1');
}
static installLocalCakeTool(targetDirectory = new toolsDirectory_1.ToolsDirectory()) {
return __awaiter(this, void 0, void 0, function* () {
const cakeTool = platform_1.Platform.isWindows() ? `${dotnetCake}.exe` : dotnetCake;
if (targetDirectory.containsFile(cakeTool)) {
core.info(`The Cake.Tool already exists in ${targetDirectory}, skipping installation`);
return;
}
return DotNet.installLocalTool('Cake.Tool', targetDirectory);
});
}
Expand Down
8 changes: 8 additions & 0 deletions lib/platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Platform {
static isWindows() {
return process.platform === 'win32';
}
}
exports.Platform = Platform;
4 changes: 4 additions & 0 deletions lib/toolsDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const io = __importStar(require("@actions/io"));
const path = __importStar(require("path"));
class ToolsDirectory {
Expand All @@ -22,6 +23,9 @@ class ToolsDirectory {
appendFileName(fileName) {
return path.join(this.path, fileName);
}
containsFile(fileName) {
return fs.existsSync(this.appendFileName(fileName));
}
toString() {
return this.path;
}
Expand Down
9 changes: 9 additions & 0 deletions src/dotnet.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import * as core from '@actions/core';
import { exec } from '@actions/exec';
import { ToolsDirectory } from './toolsDirectory';
import { Platform } from './platform';

const dotnetToolInstall = 'dotnet tool install';
const dotnetCake = 'dotnet-cake';

export class DotNet {
static disableTelemetry() {
core.exportVariable('DOTNET_CLI_TELEMETRY_OPTOUT', '1');
}

static async installLocalCakeTool(targetDirectory: ToolsDirectory = new ToolsDirectory()) {
const cakeTool = Platform.isWindows() ? `${dotnetCake}.exe` : dotnetCake;

if (targetDirectory.containsFile(cakeTool)) {
core.info(`The Cake.Tool already exists in ${targetDirectory}, skipping installation`);
return;
}

return DotNet.installLocalTool('Cake.Tool', targetDirectory);
}

Expand Down
5 changes: 5 additions & 0 deletions src/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class Platform {
static isWindows(): boolean {
return process.platform === 'win32';
}
}
5 changes: 5 additions & 0 deletions src/toolsDirectory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs';
import * as io from '@actions/io';
import * as path from 'path';

Expand All @@ -20,6 +21,10 @@ export class ToolsDirectory {
return path.join(this.path, fileName);
}

containsFile(fileName: string): boolean {
return fs.existsSync(this.appendFileName(fileName));
}

toString(): string {
return this.path;
}
Expand Down

0 comments on commit e24c3e5

Please # to comment.