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,19 +1,45 @@
const warnMock = jest.fn()
import { FileArtifactGlobber } from "../src/ArtifactGlobber"
import { Globber } from "../src/Globber";
import { Artifact } from "../src/Artifact";
import untildify = require("untildify");
const contentType = "raw"
const globMock = jest.fn()
const globResults = ["file1", "file2"]
jest.mock('@actions/core', () => {
return {warning: warnMock};
})
describe("ArtifactGlobber", () => {
it("globs simple path", () => {
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", () => {
const globber = createArtifactGlobber()
const expectedArtifacts =
globResults.map((path) => new Artifact(path, contentType))
expect(globber.globArtifactString('path', 'raw'))
.toEqual(expectedArtifacts)
expect(globMock).toBeCalledWith('path')
expect(warnMock).not.toBeCalled()
})
it("splits multiple paths", () => {
@@ -26,14 +52,29 @@ describe("ArtifactGlobber", () => {
expect(globber.globArtifactString('path1,path2', 'raw'))
.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>(() => {
return {
glob: () => globResults
glob: globMock
}
})
globMock.mockReturnValue(results)
return new FileArtifactGlobber(new MockGlobber())
}
})

View File

@@ -3,8 +3,8 @@ import * as github from "@actions/github";
import {Inputs} from "../src/Inputs";
import {GithubReleases} from "../src/Releases";
import {GithubArtifactUploader} from "../src/ArtifactUploader";
import {Artifact} from "../src/Artifact";
import * as path from "path";
import {FileArtifactGlobber} from "../src/ArtifactGlobber";
// 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
@@ -27,28 +27,33 @@ describe.skip('Integration Test', () => {
})
function getInputs(): Inputs {
const artifactPath = path.join(__dirname, 'Integration.test.ts')
// new
const MockInputs = jest.fn<Inputs, any>(() => {
return {
allowUpdates: true,
artifacts: [new Artifact(artifactPath)],
createdReleaseBody: "release body",
createdReleaseName: "release name",
artifacts: artifacts(),
createdReleaseBody: "This release was generated by release-action's integration test",
createdReleaseName: "Releases Action Integration Test",
commit: "",
draft: false,
owner: "ncipollo",
prerelease: true,
prerelease: false,
replacesArtifacts: true,
repo: "actions-playground",
tag: "0.0.71",
tag: "release-action-test",
token: getToken(),
updatedReleaseBody: "updated body",
updatedReleaseName: "updated name"
updatedReleaseBody: "This release was generated by release-action's integration test",
updatedReleaseName: "Releases Action Integration Test"
}
})
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 {
return process.env.GITHUB_TOKEN ?? ""

View File

@@ -1,17 +1,55 @@
"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 });
exports.FileArtifactGlobber = void 0;
const core = __importStar(require("@actions/core"));
const Globber_1 = require("./Globber");
const Artifact_1 = require("./Artifact");
const untildify_1 = __importDefault(require("untildify"));
class FileArtifactGlobber {
constructor(globber = new Globber_1.FileGlobber()) {
this.globber = globber;
}
globArtifactString(artifact, contentType) {
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))
.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;

View File

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

View File

@@ -1,5 +1,7 @@
import { Globber, FileGlobber } from "./Globber";
import { Artifact } from "./Artifact";
import * as core from '@actions/core';
import {Globber, FileGlobber} from "./Globber";
import {Artifact} from "./Artifact";
import untildify from "untildify";
export interface ArtifactGlobber {
globArtifactString(artifact: string, contentType: string): Artifact[]
@@ -14,9 +16,25 @@ export class FileArtifactGlobber implements ArtifactGlobber {
globArtifactString(artifact: string, contentType: string): Artifact[] {
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))
.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"
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:
version "4.4.1"
resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e"