@ -1,5 +1,6 @@
import * as core from "@actions/core" ;
import * as core from "@actions/core" ;
import * as fs from "fs" ;
import * as io from "@actions/io" ;
import { promises as fs } from "fs" ;
import * as os from "os" ;
import * as os from "os" ;
import * as path from "path" ;
import * as path from "path" ;
@ -7,13 +8,24 @@ import { Events, Outputs, State } from "../src/constants";
import { ArtifactCacheEntry } from "../src/contracts" ;
import { ArtifactCacheEntry } from "../src/contracts" ;
import * as actionUtils from "../src/utils/actionUtils" ;
import * as actionUtils from "../src/utils/actionUtils" ;
import uuid = require ( "uuid" ) ;
jest . mock ( "@actions/core" ) ;
jest . mock ( "@actions/core" ) ;
jest . mock ( "os" ) ;
jest . mock ( "os" ) ;
function getTempDir ( ) : string {
return path . join ( __dirname , "_temp" , "actionUtils" ) ;
}
afterEach ( ( ) = > {
afterEach ( ( ) = > {
delete process . env [ Events . Key ] ;
delete process . env [ Events . Key ] ;
} ) ;
} ) ;
afterAll ( async ( ) = > {
delete process . env [ "GITHUB_WORKSPACE" ] ;
await io . rmRF ( getTempDir ( ) ) ;
} ) ;
test ( "getArchiveFileSize returns file size" , ( ) = > {
test ( "getArchiveFileSize returns file size" , ( ) = > {
const filePath = path . join ( __dirname , "__fixtures__" , "helloWorld.txt" ) ;
const filePath = path . join ( __dirname , "__fixtures__" , "helloWorld.txt" ) ;
@ -182,17 +194,43 @@ test("isValidEvent returns false for unknown event", () => {
expect ( isValidEvent ) . toBe ( false ) ;
expect ( isValidEvent ) . toBe ( false ) ;
} ) ;
} ) ;
test ( "resolvePath with no ~ in path" , ( ) = > {
test ( "resolvePaths with no ~ in path" , async ( ) = > {
const filePath = ".cache/yarn" ;
const filePath = ".cache" ;
// Create the following layout:
// cwd
// cwd/.cache
// cwd/.cache/file.txt
const root = path . join ( getTempDir ( ) , "no-tilde" ) ;
// tarball entries will be relative to workspace
process . env [ "GITHUB_WORKSPACE" ] = root ;
await fs . mkdir ( root , { recursive : true } ) ;
const cache = path . join ( root , ".cache" ) ;
await fs . mkdir ( cache , { recursive : true } ) ;
await fs . writeFile ( path . join ( cache , "file.txt" ) , "cached" ) ;
const resolvedPath = actionUtils . resolvePath ( filePath ) ;
const originalCwd = process . cwd ( ) ;
const expectedPath = path . resolve ( filePath ) ;
try {
expect ( resolvedPath ) . toBe ( expectedPath ) ;
process . chdir ( root ) ;
const resolvedPath = await actionUtils . resolvePaths ( [ filePath ] ) ;
const expectedPath = [ filePath ] ;
expect ( resolvedPath ) . toStrictEqual ( expectedPath ) ;
} finally {
process . chdir ( originalCwd ) ;
}
} ) ;
} ) ;
test ( "resolvePath with ~ in path" , ( ) = > {
test ( "resolvePaths with ~ in path" , async ( ) = > {
const filePath = "~/.cache/yarn" ;
const cacheDir = uuid ( ) ;
const filePath = ` ~/ ${ cacheDir } ` ;
// Create the following layout:
// ~/uuid
// ~/uuid/file.txt
const homedir = jest . requireActual ( "os" ) . homedir ( ) ;
const homedir = jest . requireActual ( "os" ) . homedir ( ) ;
const homedirMock = jest . spyOn ( os , "homedir" ) ;
const homedirMock = jest . spyOn ( os , "homedir" ) ;
@ -200,24 +238,93 @@ test("resolvePath with ~ in path", () => {
return homedir ;
return homedir ;
} ) ;
} ) ;
const resolvedPath = actionUtils . resolvePath ( filePath ) ;
const target = path . join ( homedir , cacheDir ) ;
await fs . mkdir ( target , { recursive : true } ) ;
await fs . writeFile ( path . join ( target , "file.txt" ) , "cached" ) ;
const root = getTempDir ( ) ;
process . env [ "GITHUB_WORKSPACE" ] = root ;
const expectedPath = path . join ( homedir , ".cache/yarn" ) ;
try {
expect ( resolvedPath ) . toBe ( expectedPath ) ;
const resolvedPath = await actionUtils . resolvePaths ( [ filePath ] ) ;
const expectedPath = [ path . relative ( root , target ) ] ;
expect ( resolvedPath ) . toStrictEqual ( expectedPath ) ;
} finally {
await io . rmRF ( target ) ;
}
} ) ;
} ) ;
test ( "resolvePath with home not found" , ( ) = > {
test ( "resolvePath s with home not found", async ( ) = > {
const filePath = "~/.cache/yarn" ;
const filePath = "~/.cache/yarn" ;
const homedirMock = jest . spyOn ( os , "homedir" ) ;
const homedirMock = jest . spyOn ( os , "homedir" ) ;
homedirMock . mockImplementation ( ( ) = > {
homedirMock . mockImplementation ( ( ) = > {
return "" ;
return "" ;
} ) ;
} ) ;
expect ( ( ) = > actionUtils . resolvePath ( filePath )) . toThrow (
await expect ( actionUtils . resolvePath s ( [ filePath ] )) . rejects . toThrow (
"Unable to resolve `~` to HOME "
"Unable to determine HOME directory "
) ;
) ;
} ) ;
} ) ;
test ( "resolvePaths inclusion pattern returns found" , async ( ) = > {
const pattern = "*.ts" ;
// Create the following layout:
// inclusion-patterns
// inclusion-patterns/miss.txt
// inclusion-patterns/test.ts
const root = path . join ( getTempDir ( ) , "inclusion-patterns" ) ;
// tarball entries will be relative to workspace
process . env [ "GITHUB_WORKSPACE" ] = root ;
await fs . mkdir ( root , { recursive : true } ) ;
await fs . writeFile ( path . join ( root , "miss.txt" ) , "no match" ) ;
await fs . writeFile ( path . join ( root , "test.ts" ) , "match" ) ;
const originalCwd = process . cwd ( ) ;
try {
process . chdir ( root ) ;
const resolvedPath = await actionUtils . resolvePaths ( [ pattern ] ) ;
const expectedPath = [ "test.ts" ] ;
expect ( resolvedPath ) . toStrictEqual ( expectedPath ) ;
} finally {
process . chdir ( originalCwd ) ;
}
} ) ;
test ( "resolvePaths exclusion pattern returns not found" , async ( ) = > {
const patterns = [ "*.ts" , "!test.ts" ] ;
// Create the following layout:
// exclusion-patterns
// exclusion-patterns/miss.txt
// exclusion-patterns/test.ts
const root = path . join ( getTempDir ( ) , "exclusion-patterns" ) ;
// tarball entries will be relative to workspace
process . env [ "GITHUB_WORKSPACE" ] = root ;
await fs . mkdir ( root , { recursive : true } ) ;
await fs . writeFile ( path . join ( root , "miss.txt" ) , "no match" ) ;
await fs . writeFile ( path . join ( root , "test.ts" ) , "no match" ) ;
const originalCwd = process . cwd ( ) ;
try {
process . chdir ( root ) ;
const resolvedPath = await actionUtils . resolvePaths ( patterns ) ;
const expectedPath = [ ] ;
expect ( resolvedPath ) . toStrictEqual ( expectedPath ) ;
} finally {
process . chdir ( originalCwd ) ;
}
} ) ;
test ( "isValidEvent returns true for push event" , ( ) = > {
test ( "isValidEvent returns true for push event" , ( ) = > {
const event = Events . Push ;
const event = Events . Push ;
process . env [ Events . Key ] = event ;
process . env [ Events . Key ] = event ;
@ -237,13 +344,14 @@ test("isValidEvent returns true for pull request event", () => {
} ) ;
} ) ;
test ( "unlinkFile unlinks file" , async ( ) = > {
test ( "unlinkFile unlinks file" , async ( ) = > {
const testDirectory = fs . mkdtemp Sync ( "unlinkFileTest" ) ;
const testDirectory = await fs . mkdtemp ( "unlinkFileTest" ) ;
const testFile = path . join ( testDirectory , "test.txt" ) ;
const testFile = path . join ( testDirectory , "test.txt" ) ;
fs . writeFile Sync ( testFile , "hello world" ) ;
await fs . writeFile ( testFile , "hello world" ) ;
await actionUtils . unlinkFile ( testFile ) ;
await actionUtils . unlinkFile ( testFile ) ;
expect ( fs . existsSync ( testFile ) ) . toBe ( false ) ;
// This should throw as testFile should not exist
await expect ( fs . stat ( testFile ) ) . rejects . toThrow ( ) ;
fs . rmdir Sync ( testDirectory ) ;
await fs . rmdir ( testDirectory ) ;
} ) ;
} ) ;