Fixes #32 Globber will now expand tilde and warn if no results are found

This commit is contained in:
Nick Cipollo
2021-03-12 12:39:55 -05:00
parent f7b694c6d5
commit 5b1448e480
6 changed files with 129 additions and 21 deletions

View File

@@ -1,11 +1,35 @@
const warnMock = jest.fn()
import { FileArtifactGlobber } from "../src/ArtifactGlobber" import { FileArtifactGlobber } from "../src/ArtifactGlobber"
import { Globber } from "../src/Globber"; import { Globber } from "../src/Globber";
import { Artifact } from "../src/Artifact"; import { Artifact } from "../src/Artifact";
import untildify = require("untildify");
const contentType = "raw" const contentType = "raw"
const globMock = jest.fn()
const globResults = ["file1", "file2"] const globResults = ["file1", "file2"]
jest.mock('@actions/core', () => {
return {warning: warnMock};
})
describe("ArtifactGlobber", () => { describe("ArtifactGlobber", () => {
beforeEach(() => {
globMock.mockClear()
})
it("expands paths in which start with a ~", () => {
const globber = createArtifactGlobber()
const expectedArtifacts =
globResults.map((path) => new Artifact(path, contentType))
expect(globber.globArtifactString('~/path', 'raw'))
.toEqual(expectedArtifacts)
expect(globMock).toBeCalledWith(untildify('~/path'))
expect(warnMock).not.toBeCalled()
})
it("globs simple path", () => { it("globs simple path", () => {
const globber = createArtifactGlobber() const globber = createArtifactGlobber()
@@ -14,6 +38,8 @@ describe("ArtifactGlobber", () => {
expect(globber.globArtifactString('path', 'raw')) expect(globber.globArtifactString('path', 'raw'))
.toEqual(expectedArtifacts) .toEqual(expectedArtifacts)
expect(globMock).toBeCalledWith('path')
expect(warnMock).not.toBeCalled()
}) })
it("splits multiple paths", () => { it("splits multiple paths", () => {
@@ -26,14 +52,29 @@ describe("ArtifactGlobber", () => {
expect(globber.globArtifactString('path1,path2', 'raw')) expect(globber.globArtifactString('path1,path2', 'raw'))
.toEqual(expectedArtifacts) .toEqual(expectedArtifacts)
expect(globMock).toBeCalledWith('path1')
expect(globMock).toBeCalledWith('path2')
expect(warnMock).not.toBeCalled()
}) })
function createArtifactGlobber(): FileArtifactGlobber { it("warns when no glob results are produced", () => {
const globber = createArtifactGlobber([])
const expectedArtifacts =
globResults.map((path) => new Artifact(path, contentType))
expect(globber.globArtifactString('path', 'raw'))
.toEqual([])
expect(warnMock).toBeCalled()
})
function createArtifactGlobber(results: string[] = globResults): FileArtifactGlobber {
const MockGlobber = jest.fn<Globber, any>(() => { const MockGlobber = jest.fn<Globber, any>(() => {
return { return {
glob: () => globResults glob: globMock
} }
}) })
globMock.mockReturnValue(results)
return new FileArtifactGlobber(new MockGlobber()) return new FileArtifactGlobber(new MockGlobber())
} }
}) })

View File

@@ -3,8 +3,8 @@ import * as github from "@actions/github";
import {Inputs} from "../src/Inputs"; import {Inputs} from "../src/Inputs";
import {GithubReleases} from "../src/Releases"; import {GithubReleases} from "../src/Releases";
import {GithubArtifactUploader} from "../src/ArtifactUploader"; import {GithubArtifactUploader} from "../src/ArtifactUploader";
import {Artifact} from "../src/Artifact";
import * as path from "path"; import * as path from "path";
import {FileArtifactGlobber} from "../src/ArtifactGlobber";
// This test is currently intended to be manually run during development. To run: // This test is currently intended to be manually run during development. To run:
// - Make sure you have an environment variable named GITHUB_TOKEN assigned to your token // - Make sure you have an environment variable named GITHUB_TOKEN assigned to your token
@@ -27,29 +27,34 @@ describe.skip('Integration Test', () => {
}) })
function getInputs(): Inputs { function getInputs(): Inputs {
const artifactPath = path.join(__dirname, 'Integration.test.ts')
// new
const MockInputs = jest.fn<Inputs, any>(() => { const MockInputs = jest.fn<Inputs, any>(() => {
return { return {
allowUpdates: true, allowUpdates: true,
artifacts: [new Artifact(artifactPath)], artifacts: artifacts(),
createdReleaseBody: "release body", createdReleaseBody: "This release was generated by release-action's integration test",
createdReleaseName: "release name", createdReleaseName: "Releases Action Integration Test",
commit: "", commit: "",
draft: false, draft: false,
owner: "ncipollo", owner: "ncipollo",
prerelease: true, prerelease: false,
replacesArtifacts: true, replacesArtifacts: true,
repo: "actions-playground", repo: "actions-playground",
tag: "0.0.71", tag: "release-action-test",
token: getToken(), token: getToken(),
updatedReleaseBody: "updated body", updatedReleaseBody: "This release was generated by release-action's integration test",
updatedReleaseName: "updated name" updatedReleaseName: "Releases Action Integration Test"
} }
}) })
return new MockInputs(); return new MockInputs();
} }
function artifacts() {
const globber = new FileArtifactGlobber()
const artifactPath = path.join(__dirname, 'Integration.test.ts')
const artifactString = `~/Desktop/test.txt,blarg.tx, ${artifactPath}`
return globber.globArtifactString(artifactString, "raw")
}
function getToken(): string { function getToken(): string {
return process.env.GITHUB_TOKEN ?? "" return process.env.GITHUB_TOKEN ?? ""
} }

View File

@@ -1,17 +1,55 @@
"use strict"; "use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.FileArtifactGlobber = void 0; exports.FileArtifactGlobber = void 0;
const core = __importStar(require("@actions/core"));
const Globber_1 = require("./Globber"); const Globber_1 = require("./Globber");
const Artifact_1 = require("./Artifact"); const Artifact_1 = require("./Artifact");
const untildify_1 = __importDefault(require("untildify"));
class FileArtifactGlobber { class FileArtifactGlobber {
constructor(globber = new Globber_1.FileGlobber()) { constructor(globber = new Globber_1.FileGlobber()) {
this.globber = globber; this.globber = globber;
} }
globArtifactString(artifact, contentType) { globArtifactString(artifact, contentType) {
return artifact.split(',') return artifact.split(',')
.map((path) => this.globber.glob(path)) .map(path => FileArtifactGlobber.expandPath(path))
.map(pattern => this.globPattern(pattern))
.reduce((accumulated, current) => accumulated.concat(current)) .reduce((accumulated, current) => accumulated.concat(current))
.map((path) => new Artifact_1.Artifact(path, contentType)); .map(path => new Artifact_1.Artifact(path, contentType));
}
globPattern(pattern) {
const paths = this.globber.glob(pattern);
if (paths.length == 0) {
FileArtifactGlobber.reportGlobWarning(pattern);
}
return paths;
}
static reportGlobWarning(pattern) {
core.warning(`Artifact pattern :${pattern} did not match any files`);
}
static expandPath(path) {
return untildify_1.default(path);
} }
} }
exports.FileArtifactGlobber = FileArtifactGlobber; exports.FileArtifactGlobber = FileArtifactGlobber;

View File

@@ -26,7 +26,8 @@
"@actions/core": "^1.2.6", "@actions/core": "^1.2.6",
"@actions/github": "^4.0.0", "@actions/github": "^4.0.0",
"@types/glob": "^7.1.3", "@types/glob": "^7.1.3",
"glob": "^7.1.4" "glob": "^7.1.4",
"untildify": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^26.0.20", "@types/jest": "^26.0.20",

View File

@@ -1,5 +1,7 @@
import * as core from '@actions/core';
import {Globber, FileGlobber} from "./Globber"; import {Globber, FileGlobber} from "./Globber";
import {Artifact} from "./Artifact"; import {Artifact} from "./Artifact";
import untildify from "untildify";
export interface ArtifactGlobber { export interface ArtifactGlobber {
globArtifactString(artifact: string, contentType: string): Artifact[] globArtifactString(artifact: string, contentType: string): Artifact[]
@@ -14,9 +16,25 @@ export class FileArtifactGlobber implements ArtifactGlobber {
globArtifactString(artifact: string, contentType: string): Artifact[] { globArtifactString(artifact: string, contentType: string): Artifact[] {
return artifact.split(',') return artifact.split(',')
.map((path) => this.globber.glob(path)) .map(path => FileArtifactGlobber.expandPath(path))
.map(pattern => this.globPattern(pattern))
.reduce((accumulated, current) => accumulated.concat(current)) .reduce((accumulated, current) => accumulated.concat(current))
.map((path) => new Artifact(path, contentType)) .map(path => new Artifact(path, contentType))
}
} }
private globPattern(pattern: string): string[] {
const paths = this.globber.glob(pattern)
if (paths.length == 0) {
FileArtifactGlobber.reportGlobWarning(pattern)
}
return paths
}
private static reportGlobWarning(pattern: string) {
core.warning(`Artifact pattern :${pattern} did not match any files`)
}
private static expandPath(path: string): string {
return untildify(path)
}
}

View File

@@ -3613,6 +3613,11 @@ unset-value@^1.0.0:
has-value "^0.3.1" has-value "^0.3.1"
isobject "^3.0.0" isobject "^3.0.0"
untildify@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/untildify/-/untildify-4.0.0.tgz#2bc947b953652487e4600949fb091e3ae8cd919b"
integrity sha512-KK8xQ1mkzZeg9inewmFVDNkg3l5LUhoq9kN6iWYB/CC9YMG8HA+c1Q8HwDe6dEX7kErrEVNVBO3fWsVq5iDgtw==
uri-js@^4.2.2: uri-js@^4.2.2:
version "4.4.1" version "4.4.1"
resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e" resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e"