Add checking if the specified file exist

This commit is contained in:
jojo43 2022-05-01 12:09:36 +09:00
parent dbe1872ad4
commit 1fefa4e324
2 changed files with 20 additions and 13 deletions

View file

@ -778,9 +778,7 @@ describe('setup-go', () => {
}); });
describe('go-version-file', () => { describe('go-version-file', () => {
it('reads version from go.mod', async () => { const goModContents = `module example.com/mymodule
inputs['go-version-file'] = 'go.mod';
const content = `module example.com/mymodule
go 1.14 go 1.14
@ -793,7 +791,11 @@ require (
replace example.com/thatmodule => ../thatmodule replace example.com/thatmodule => ../thatmodule
exclude example.com/thismodule v1.3.0 exclude example.com/thismodule v1.3.0
`; `;
readFileSpy.mockImplementation(() => Buffer.from(content));
it('reads version from go.mod', async () => {
inputs['go-version-file'] = 'go.mod';
existsSpy.mockImplementation(path => true);
readFileSpy.mockImplementation(() => Buffer.from(goModContents));
await main.run(); await main.run();
@ -804,6 +806,7 @@ exclude example.com/thismodule v1.3.0
it('reads version from .go-version', async () => { it('reads version from .go-version', async () => {
inputs['go-version-file'] = '.go-version'; inputs['go-version-file'] = '.go-version';
existsSpy.mockImplementation(path => true);
readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`)); readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`));
await main.run(); await main.run();
@ -815,8 +818,9 @@ exclude example.com/thismodule v1.3.0
it('is overwritten by go-version', async () => { it('is overwritten by go-version', async () => {
inputs['go-version'] = '1.13.1'; inputs['go-version'] = '1.13.1';
inputs['go-version-file'] = '.go-version'; inputs['go-version-file'] = 'go.mod';
readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`)); existsSpy.mockImplementation(path => true);
readFileSpy.mockImplementation(() => Buffer.from(goModContents));
await main.run(); await main.run();
@ -826,16 +830,14 @@ exclude example.com/thismodule v1.3.0
}); });
it('reports a read failure', async () => { it('reports a read failure', async () => {
const versionFilePath = '.go-version'; inputs['go-version-file'] = 'go.mod';
inputs['go-version-file'] = versionFilePath; existsSpy.mockImplementation(path => false);
const errMsg = `ENOENT: no such file or directory, open '${versionFilePath}'`;
readFileSpy.mockImplementation(() => {
throw new Error(errMsg);
});
await main.run(); await main.run();
expect(cnSpy).toHaveBeenCalledWith(`::error::${errMsg}${osm.EOL}`); expect(cnSpy).toHaveBeenCalledWith(
`::error::The specified go version file at: go.mod does not exist${osm.EOL}`
);
}); });
}); });
}); });

View file

@ -120,6 +120,11 @@ function resolveVersionInput(): string {
} }
if (versionFilePath) { if (versionFilePath) {
if (!fs.existsSync(versionFilePath)) {
throw new Error(
`The specified go version file at: ${versionFilePath} does not exist`
);
}
version = installer.parseGoVersionFile(versionFilePath); version = installer.parseGoVersionFile(versionFilePath);
} }