-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove toolchain directories from the cache
- Loading branch information
Showing
16 changed files
with
527 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: Validate 'setup-go' | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
paths-ignore: | ||
- '**.md' | ||
pull_request: | ||
paths-ignore: | ||
- '**.md' | ||
schedule: | ||
- cron: 0 0 * * * | ||
|
||
jobs: | ||
local-cache: | ||
name: Setup local-cache version | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
os: [macos-latest, windows-latest, ubuntu-latest] | ||
go: [1.21] | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: substitute go.mod with toolchain | ||
run: | | ||
cp __tests__/toolchain.go.mod go.mod | ||
shell: bash | ||
|
||
- name: setup-go ${{ matrix.go }} | ||
uses: ./ | ||
with: | ||
go-version: ${{ matrix.go }} | ||
|
||
- name: verify go | ||
run: __tests__/verify-go.sh ${{ matrix.go }} | ||
shell: bash |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ import * as cache from '@actions/cache'; | |
import * as core from '@actions/core'; | ||
import * as cacheUtils from '../src/cache-utils'; | ||
import {PackageManagerInfo} from '../src/package-managers'; | ||
import fs, {ObjectEncodingOptions, PathLike} from 'fs'; | ||
import {getToolchainDirectoriesFromCachedDirectories} from '../src/cache-utils'; | ||
|
||
describe('getCommandOutput', () => { | ||
//Arrange | ||
|
@@ -209,3 +211,178 @@ describe('isCacheFeatureAvailable', () => { | |
expect(warningSpy).toHaveBeenCalledWith(warningMessage); | ||
}); | ||
}); | ||
|
||
describe('parseGoModForToolchainVersion', () => { | ||
const readFileSyncSpy = jest.spyOn(fs, 'readFileSync'); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return null when go.mod file not exist', async () => { | ||
//Arrange | ||
//Act | ||
const toolchainVersion = cacheUtils.parseGoModForToolchainVersion( | ||
'/tmp/non/exist/foo.bar' | ||
); | ||
//Assert | ||
expect(toolchainVersion).toBeNull(); | ||
}); | ||
|
||
it('should return null when go.mod file is empty', async () => { | ||
//Arrange | ||
readFileSyncSpy.mockImplementation(() => ''); | ||
//Act | ||
const toolchainVersion = cacheUtils.parseGoModForToolchainVersion('go.mod'); | ||
//Assert | ||
expect(toolchainVersion).toBeNull(); | ||
}); | ||
|
||
it('should return null when go.mod file does not contain toolchain version', async () => { | ||
//Arrange | ||
readFileSyncSpy.mockImplementation(() => | ||
` | ||
module example-mod | ||
go 1.21.0 | ||
require golang.org/x/tools v0.13.0 | ||
require ( | ||
golang.org/x/mod v0.12.0 // indirect | ||
golang.org/x/sys v0.12.0 // indirect | ||
) | ||
`.replace(/^\s+/gm, '') | ||
); | ||
//Act | ||
const toolchainVersion = cacheUtils.parseGoModForToolchainVersion('go.mod'); | ||
//Assert | ||
expect(toolchainVersion).toBeNull(); | ||
}); | ||
|
||
it('should return go version when go.mod file contains go version', () => { | ||
//Arrange | ||
readFileSyncSpy.mockImplementation(() => | ||
` | ||
module example-mod | ||
go 1.21.0 | ||
toolchain go1.21.1 | ||
require golang.org/x/tools v0.13.0 | ||
require ( | ||
golang.org/x/mod v0.12.0 // indirect | ||
golang.org/x/sys v0.12.0 // indirect | ||
) | ||
`.replace(/^\s+/gm, '') | ||
); | ||
|
||
//Act | ||
const toolchainVersion = cacheUtils.parseGoModForToolchainVersion('go.mod'); | ||
//Assert | ||
expect(toolchainVersion).toBe('1.21.1'); | ||
}); | ||
|
||
it('should return go version when go.mod file contains more than one go version', () => { | ||
//Arrange | ||
readFileSyncSpy.mockImplementation(() => | ||
` | ||
module example-mod | ||
go 1.21.0 | ||
toolchain go1.21.0 | ||
toolchain go1.21.1 | ||
require golang.org/x/tools v0.13.0 | ||
require ( | ||
golang.org/x/mod v0.12.0 // indirect | ||
golang.org/x/sys v0.12.0 // indirect | ||
) | ||
`.replace(/^\s+/gm, '') | ||
); | ||
|
||
//Act | ||
const toolchainVersion = cacheUtils.parseGoModForToolchainVersion('go.mod'); | ||
//Assert | ||
expect(toolchainVersion).toBe('1.21.1'); | ||
}); | ||
}); | ||
|
||
describe('getToolchainDirectoriesFromCachedDirectories', () => { | ||
const readdirSyncSpy = jest.spyOn(fs, 'readdirSync'); | ||
const existsSyncSpy = jest.spyOn(fs, 'existsSync'); | ||
const lstatSync = jest.spyOn(fs, 'lstatSync'); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return empty array when cacheDirectories is empty', async () => { | ||
const toolcacheDirectories = getToolchainDirectoriesFromCachedDirectories( | ||
'foo', | ||
[] | ||
); | ||
expect(toolcacheDirectories).toEqual([]); | ||
}); | ||
|
||
it('should return empty array when cacheDirectories does not contain /go/pkg', async () => { | ||
readdirSyncSpy.mockImplementation(dir => | ||
[`${dir}1`, `${dir}2`, `${dir}3`].map(s => { | ||
const de = new fs.Dirent(); | ||
de.name = s; | ||
de.isDirectory = () => true; | ||
return de; | ||
}) | ||
); | ||
|
||
const toolcacheDirectories = getToolchainDirectoriesFromCachedDirectories( | ||
'1.1.1', | ||
['foo', 'bar'] | ||
); | ||
expect(toolcacheDirectories).toEqual([]); | ||
}); | ||
|
||
it('should return empty array when cacheDirectories does not contain toolchain@v[0-9.]+-go{goVersion}', async () => { | ||
readdirSyncSpy.mockImplementation(dir => | ||
[`${dir}1`, `${dir}2`, `${dir}3`].map(s => { | ||
const de = new fs.Dirent(); | ||
de.name = s; | ||
de.isDirectory = () => true; | ||
return de; | ||
}) | ||
); | ||
|
||
const toolcacheDirectories = getToolchainDirectoriesFromCachedDirectories( | ||
'foo', | ||
['foo/go/pkg/mod', 'bar'] | ||
); | ||
expect(toolcacheDirectories).toEqual([]); | ||
}); | ||
|
||
it('should return one entry when cacheDirectories contains toolchain@v[0-9.]+-go{goVersion} in /pkg/mod', async () => { | ||
let seqNo = 1; | ||
readdirSyncSpy.mockImplementation(dir => | ||
[`[email protected]${seqNo++}`].map(s => { | ||
const de = new fs.Dirent(); | ||
de.name = s; | ||
de.isDirectory = () => true; | ||
return de; | ||
}) | ||
); | ||
existsSyncSpy.mockReturnValue(true); | ||
// @ts-ignore - jest does not have relaxed mocks, so we ignore not-implemented methods | ||
lstatSync.mockImplementation(() => ({isDirectory: () => true})); | ||
|
||
const toolcacheDirectories = getToolchainDirectoriesFromCachedDirectories( | ||
'1.1.1', | ||
['/foo/go/pkg/mod', 'bar'] | ||
); | ||
expect(toolcacheDirectories).toEqual([ | ||
'/foo/go/pkg/mod/golang.org/[email protected]' | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module example-mod | ||
|
||
go 1.21.0 | ||
|
||
toolchain go1.21.0 | ||
toolchain go1.21.1 | ||
|
||
require golang.org/x/tools v0.13.0 | ||
|
||
require ( | ||
golang.org/x/mod v0.12.0 // indirect | ||
golang.org/x/sys v0.12.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {isSelfHosted} from '../src/utils'; | ||
|
||
describe('utils', () => { | ||
describe('isSelfHosted', () => { | ||
let AGENT_ISSELFHOSTED: string | undefined; | ||
let RUNNER_ENVIRONMENT: string | undefined; | ||
|
||
beforeEach(() => { | ||
AGENT_ISSELFHOSTED = process.env['AGENT_ISSELFHOSTED']; | ||
delete process.env['AGENT_ISSELFHOSTED']; | ||
RUNNER_ENVIRONMENT = process.env['RUNNER_ENVIRONMENT']; | ||
delete process.env['RUNNER_ENVIRONMENT']; | ||
}); | ||
|
||
afterEach(() => { | ||
if (AGENT_ISSELFHOSTED === undefined) { | ||
delete process.env['AGENT_ISSELFHOSTED']; | ||
} else { | ||
process.env['AGENT_ISSELFHOSTED'] = AGENT_ISSELFHOSTED; | ||
} | ||
if (RUNNER_ENVIRONMENT === undefined) { | ||
delete process.env['RUNNER_ENVIRONMENT']; | ||
} else { | ||
process.env['RUNNER_ENVIRONMENT'] = RUNNER_ENVIRONMENT; | ||
} | ||
}); | ||
|
||
it('isSelfHosted should be true if no environment variables set', () => { | ||
expect(isSelfHosted()).toBeTruthy(); | ||
}); | ||
|
||
it('isSelfHosted should be true if environment variable is not set to denote GitHub hosted', () => { | ||
process.env['RUNNER_ENVIRONMENT'] = 'some'; | ||
expect(isSelfHosted()).toBeTruthy(); | ||
}); | ||
|
||
it('isSelfHosted should be true if environment variable set to denote Azure Pipelines self hosted', () => { | ||
process.env['AGENT_ISSELFHOSTED'] = '1'; | ||
expect(isSelfHosted()).toBeTruthy(); | ||
}); | ||
|
||
it('isSelfHosted should be false if environment variable set to denote GitHub hosted', () => { | ||
process.env['RUNNER_ENVIRONMENT'] = 'github-hosted'; | ||
expect(isSelfHosted()).toBeFalsy(); | ||
}); | ||
|
||
it('isSelfHosted should be false if environment variable is not set to denote Azure Pipelines self hosted', () => { | ||
process.env['AGENT_ISSELFHOSTED'] = 'some'; | ||
expect(isSelfHosted()).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.