utilize .tool-versions file to resolve terraform version

This commit is contained in:
plukevdh 2023-05-23 15:50:27 -04:00
parent 1fdd4cd311
commit 5de63cedb5
No known key found for this signature in database
GPG key ID: 4CDF66F8A7057826
4 changed files with 158 additions and 5 deletions

View file

@ -156,7 +156,7 @@ describe('Setup Terraform', () => {
.reply(200, json);
const versionObj = await setup();
expect(versionObj.version).toEqual('0.10.0');
expect(versionObj.version).toEqual('1.3.7');
// downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled();
@ -420,6 +420,59 @@ describe('Setup Terraform', () => {
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
test('gets version from .tool-versions when version not provided', async () => {
const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl';
fs.existsSync = jest
.fn()
.mockReturnValueOnce(true);
fs.readFileSync = jest
.fn()
.mockReturnValueOnce(`terraform 1.3.7
golang 1.16.3
python 3.10.11
ruby 3.2.1
`);
core.getInput = jest
.fn()
.mockReturnValueOnce(null)
.mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken);
tc.downloadTool = jest
.fn()
.mockReturnValueOnce('file.zip');
tc.extractZip = jest
.fn()
.mockReturnValueOnce('file');
os.platform = jest
.fn()
.mockReturnValue('linux');
os.arch = jest
.fn()
.mockReturnValue('amd64');
nock('https://releases.hashicorp.com')
.get('/terraform/index.json')
.reply(200, json);
const versionObj = await setup();
expect(versionObj.version).toEqual('1.3.7');
// downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled();
// expect credentials are in ${HOME}.terraformrc
const creds = await fs.readFile(`${process.env.HOME}/.terraformrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
test('fails when metadata cannot be downloaded', async () => {
const version = 'latest';
const credentialsHostname = 'app.terraform.io';