Skip to content

Commit

Permalink
Change variable path to a list
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiqiao Yan authored and Aiqiao Yan committed May 12, 2020
1 parent 932779c commit 7409ad5
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 411 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ $ npm install @actions/artifact --save
Provides functions to interact with actions cache. Read more [here](packages/cache)

```bash
$ npm install @actions/artifact --save
$ npm install @actions/cache --save
```
<br/>

Expand Down
12 changes: 12 additions & 0 deletions packages/cache/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# `@actions/cache`

> Functions necessary for caching dependencies and build outputs to improve workflow execution time.
## Usage

#### Restore Cache

#### Save Cache

## Additional Documentation

See ["Caching dependencies to speed up workflows"](https://help.github.com/github/automating-your-workflow-with-github-actions/caching-dependencies-to-speed-up-workflows).
22 changes: 15 additions & 7 deletions packages/cache/__tests__/cacheHttpClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import {getCacheVersion} from '../src/internal/cacheHttpClient'
import {CompressionMethod} from '../src/internal/constants'

test('getCacheVersion with path input and compression method undefined returns version', async () => {
const inputPath = 'node_modules'
const result = getCacheVersion(inputPath)
test('getCacheVersion with one path returns version', async () => {
const paths = ['node_modules']
const result = getCacheVersion(paths)
expect(result).toEqual(
'b3e0c6cb5ecf32614eeb2997d905b9c297046d7cbf69062698f25b14b4cb0985'
)
})

test('getCacheVersion with multiple paths returns version', async () => {
const paths = ['node_modules', 'dist']
const result = getCacheVersion(paths)
expect(result).toEqual(
'165c3053bc646bf0d4fac17b1f5731caca6fe38e0e464715c0c3c6b6318bf436'
)
})

test('getCacheVersion with zstd compression returns version', async () => {
const inputPath = 'node_modules'
const result = getCacheVersion(inputPath, CompressionMethod.Zstd)
const paths = ['node_modules']
const result = getCacheVersion(paths, CompressionMethod.Zstd)

expect(result).toEqual(
'273877e14fd65d270b87a198edbfa2db5a43de567c9a548d2a2505b408befe24'
)
})

test('getCacheVersion with gzip compression does not change vesion', async () => {
const inputPath = 'node_modules'
const result = getCacheVersion(inputPath, CompressionMethod.Gzip)
const paths = ['node_modules']
const result = getCacheVersion(paths, CompressionMethod.Gzip)

expect(result).toEqual(
'b3e0c6cb5ecf32614eeb2997d905b9c297046d7cbf69062698f25b14b4cb0985'
Expand Down
140 changes: 0 additions & 140 deletions packages/cache/__tests__/cacheUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as core from '@actions/core'
import * as io from '@actions/io'
import {promises as fs} from 'fs'
import * as os from 'os'
import * as path from 'path'
import {v4 as uuidV4} from 'uuid'
import * as cacheUtils from '../src/internal/cacheUtils'

jest.mock('@actions/core')
Expand All @@ -26,143 +23,6 @@ test('getArchiveFileSize returns file size', () => {
expect(size).toBe(11)
})

test('logWarning logs a message with a warning prefix', () => {
const message = 'A warning occurred.'

const infoMock = jest.spyOn(core, 'info')

cacheUtils.logWarning(message)

expect(infoMock).toHaveBeenCalledWith(`[warning]${message}`)
})

test('resolvePaths with no ~ in path', async () => {
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 originalCwd = process.cwd()

try {
process.chdir(root)

const resolvedPath = await cacheUtils.resolvePaths([filePath])

const expectedPath = [filePath]
expect(resolvedPath).toStrictEqual(expectedPath)
} finally {
process.chdir(originalCwd)
}
})

test('resolvePaths with ~ in path', async () => {
const cacheDir = uuidV4()
const filePath = `~/${cacheDir}`
// Create the following layout:
// ~/uuid
// ~/uuid/file.txt

const homedir = jest.requireActual('os').homedir()
const homedirMock = jest.spyOn(os, 'homedir')
homedirMock.mockReturnValue(homedir)

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

try {
const resolvedPath = await cacheUtils.resolvePaths([filePath])

const expectedPath = [path.relative(root, target)]
expect(resolvedPath).toStrictEqual(expectedPath)
} finally {
await io.rmRF(target)
}
})

test('resolvePaths with home not found', async () => {
const filePath = '~/.cache/yarn'
const homedirMock = jest.spyOn(os, 'homedir')
homedirMock.mockReturnValue('')

await expect(cacheUtils.resolvePaths([filePath])).rejects.toThrow(
'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 cacheUtils.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 cacheUtils.resolvePaths(patterns)

const expectedPath: string[] = []
expect(resolvedPath).toStrictEqual(expectedPath)
} finally {
process.chdir(originalCwd)
}
})

test('unlinkFile unlinks file', async () => {
const testDirectory = await fs.mkdtemp('unlinkFileTest')
const testFile = path.join(testDirectory, 'test.txt')
Expand Down
Loading

0 comments on commit 7409ad5

Please sign in to comment.