2020-05-06 15:10:18 +00:00
|
|
|
import {promises as fs} from 'fs'
|
|
|
|
import * as path from 'path'
|
|
|
|
import * as cacheUtils from '../src/internal/cacheUtils'
|
|
|
|
|
2024-01-30 21:55:50 +00:00
|
|
|
beforeEach(() => {
|
2024-01-31 16:51:04 +00:00
|
|
|
jest.resetModules()
|
|
|
|
})
|
2024-01-30 21:55:50 +00:00
|
|
|
|
2021-05-03 15:09:44 +00:00
|
|
|
test('getArchiveFileSizeInBytes returns file size', () => {
|
2020-05-06 15:10:18 +00:00
|
|
|
const filePath = path.join(__dirname, '__fixtures__', 'helloWorld.txt')
|
|
|
|
|
2021-05-03 15:09:44 +00:00
|
|
|
const size = cacheUtils.getArchiveFileSizeInBytes(filePath)
|
2020-05-06 15:10:18 +00:00
|
|
|
|
|
|
|
expect(size).toBe(11)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('unlinkFile unlinks file', async () => {
|
|
|
|
const testDirectory = await fs.mkdtemp('unlinkFileTest')
|
|
|
|
const testFile = path.join(testDirectory, 'test.txt')
|
|
|
|
await fs.writeFile(testFile, 'hello world')
|
|
|
|
|
2020-05-15 16:18:50 +00:00
|
|
|
await expect(fs.stat(testFile)).resolves.not.toThrow()
|
|
|
|
|
2020-05-06 15:10:18 +00:00
|
|
|
await cacheUtils.unlinkFile(testFile)
|
|
|
|
|
|
|
|
// This should throw as testFile should not exist
|
|
|
|
await expect(fs.stat(testFile)).rejects.toThrow()
|
|
|
|
|
|
|
|
await fs.rmdir(testDirectory)
|
|
|
|
})
|
2020-07-10 15:09:32 +00:00
|
|
|
|
|
|
|
test('assertDefined throws if undefined', () => {
|
|
|
|
expect(() => cacheUtils.assertDefined('test', undefined)).toThrowError()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('assertDefined returns value', () => {
|
|
|
|
expect(cacheUtils.assertDefined('test', 5)).toBe(5)
|
|
|
|
})
|
2022-07-13 11:39:29 +00:00
|
|
|
|
2022-07-20 05:25:47 +00:00
|
|
|
test('resolvePaths works on github workspace directory', async () => {
|
|
|
|
const workspace = process.env['GITHUB_WORKSPACE'] ?? '.'
|
|
|
|
const paths = await cacheUtils.resolvePaths([workspace])
|
|
|
|
expect(paths.length).toBeGreaterThan(0)
|
2022-07-13 11:39:29 +00:00
|
|
|
})
|
2024-01-30 21:55:50 +00:00
|
|
|
|
|
|
|
test('isGhes returns false for github.com', async () => {
|
|
|
|
process.env.GITHUB_SERVER_URL = 'https://github.com'
|
|
|
|
expect(cacheUtils.isGhes()).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('isGhes returns false for ghe.com', async () => {
|
|
|
|
process.env.GITHUB_SERVER_URL = 'https://somedomain.ghe.com'
|
|
|
|
expect(cacheUtils.isGhes()).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('isGhes returns true for enterprise URL', async () => {
|
|
|
|
process.env.GITHUB_SERVER_URL = 'https://my-enterprise.github.com'
|
|
|
|
expect(cacheUtils.isGhes()).toBe(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('isGhes returns false for ghe.localhost', () => {
|
|
|
|
process.env.GITHUB_SERVER_URL = 'https://my.domain.ghe.localhost'
|
|
|
|
expect(cacheUtils.isGhes()).toBe(false)
|
2024-01-31 16:51:04 +00:00
|
|
|
})
|