Fix test cases when writting the wrapper

This commit is contained in:
Gerard Ribugent 2026-01-22 14:57:46 +01:00
parent 963824662d
commit 320df87442
No known key found for this signature in database
GPG key ID: DCD0E41D4DDD2F80

View file

@ -27,6 +27,9 @@ const setup = require('../lib/setup-terraform');
// core.error = jest // core.error = jest
// .fn(console.error); // .fn(console.error);
const fsReadFileImplementation = fs.readFile;
const fsWriteFileImplementation = fs.writeFile;
describe('Setup Terraform', () => { describe('Setup Terraform', () => {
const HOME = process.env.HOME; const HOME = process.env.HOME;
const APPDATA = process.env.APPDATA; const APPDATA = process.env.APPDATA;
@ -553,8 +556,20 @@ describe('Setup Terraform', () => {
const ioMv = jest.spyOn(io, 'mv') const ioMv = jest.spyOn(io, 'mv')
.mockImplementation(() => {}); .mockImplementation(() => {});
const ioCp = jest.spyOn(io, 'cp') const fsReadFile = jest.spyOn(fs, 'readFile').mockImplementation((...args) => {
.mockImplementation(() => {}); if (args[0] === wrapperPath) {
return Promise.resolve('');
} else {
return fsReadFileImplementation(...args);
}
});
const fsWriteFile = jest.spyOn(fs, 'writeFile').mockImplementation((...args) => {
if (args[0] === `file${path.sep}terraform`) {
return Promise.resolve();
} else {
return fsWriteFileImplementation(...args);
}
});
core.getInput = jest core.getInput = jest
.fn() .fn()
@ -586,7 +601,8 @@ describe('Setup Terraform', () => {
await setup(); await setup();
expect(ioMv).toHaveBeenCalledWith(`file${path.sep}terraform`, `file${path.sep}terraform-bin`); expect(ioMv).toHaveBeenCalledWith(`file${path.sep}terraform`, `file${path.sep}terraform-bin`);
expect(ioCp).toHaveBeenCalledWith(wrapperPath, `file${path.sep}terraform`); expect(fsReadFile).toHaveBeenCalledWith(wrapperPath, 'utf8');
expect(fsWriteFile).toHaveBeenCalledWith(`file${path.sep}terraform`, expect.any(String));
}); });
test('installs wrapper on windows', async () => { test('installs wrapper on windows', async () => {
@ -597,8 +613,21 @@ describe('Setup Terraform', () => {
const ioMv = jest.spyOn(io, 'mv') const ioMv = jest.spyOn(io, 'mv')
.mockImplementation(() => {}); .mockImplementation(() => {});
const ioCp = jest.spyOn(io, 'cp') const fsReadFileImplementation = fs.readFile;
.mockImplementation(() => {}); const fsReadFile = jest.spyOn(fs, 'readFile').mockImplementation((...args) => {
if (args[0] === wrapperPath) {
return Promise.resolve('');
} else {
return fsReadFileImplementation(...args);
}
});
const fsWriteFile = jest.spyOn(fs, 'writeFile').mockImplementation((...args) => {
if (args[0] === `file${path.sep}terraform`) {
return Promise.resolve();
} else {
return fsWriteFileImplementation(...args);
}
});
core.getInput = jest core.getInput = jest
.fn() .fn()
@ -630,6 +659,7 @@ describe('Setup Terraform', () => {
await setup(); await setup();
expect(ioMv).toHaveBeenCalledWith(`file${path.sep}terraform.exe`, `file${path.sep}terraform-bin.exe`); expect(ioMv).toHaveBeenCalledWith(`file${path.sep}terraform.exe`, `file${path.sep}terraform-bin.exe`);
expect(ioCp).toHaveBeenCalledWith(wrapperPath, `file${path.sep}terraform`); expect(fsReadFile).toHaveBeenCalledWith(wrapperPath, 'utf8');
expect(fsWriteFile).toHaveBeenCalledWith(`file${path.sep}terraform`, expect.any(String));
}); });
}); });