feat: add test for cleanup

This commit is contained in:
Serhii Freidin 2023-12-19 10:29:06 +02:00
parent f6ec85149b
commit 7b7ad1617b
No known key found for this signature in database
GPG key ID: A249D75F3EC64120
7 changed files with 26002 additions and 42 deletions

42
cleanup/cleanup.js Normal file
View file

@ -0,0 +1,42 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
const fs = require('fs');
const path = require('path');
const core = require('@actions/core');
async function run () {
// Retrieve environment variables and parameters
const terraformCliPath = process.env.TERRAFORM_CLI_PATH;
// This parameter should be set in `action.yaml` to the `runs.post-if` condition after solving issue https://github.com/actions/runner/issues/2800
const cleanup = core.getInput('cleanup_workspace');
// Function to recursively delete a directory
const deleteDirectoryRecursive = function (directoryPath) {
if (fs.existsSync(directoryPath)) {
fs.readdirSync(directoryPath).forEach((file) => {
const curPath = path.join(directoryPath, file);
if (fs.lstatSync(curPath).isDirectory()) {
// Recurse
deleteDirectoryRecursive(curPath);
} else {
// Delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(directoryPath);
}
};
// Check if cleanup is required
if (cleanup === 'true' && terraformCliPath) {
console.log(`Cleaning up directory: ${terraformCliPath}`);
deleteDirectoryRecursive(terraformCliPath);
console.log('Cleanup completed.');
} else {
console.log('No cleanup required.');
}
}
run();

25920
cleanup/dist/index.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,36 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
describe('Cleanup Test Suite', () => {
it('post-jobs cleanup step', async () => {
// Create test directory structure
const testDir = 'testDir';
if (!fs.existsSync(testDir)) {
fs.mkdirSync(testDir, { recursive: true });
}
fs.writeFileSync(path.join(testDir, 'testFile.txt'), 'test content');
// Call your cleanup function
execSync('node cleanup/cleanup', {
env: {
...process.env,
TERRAFORM_CLI_PATH: testDir,
INPUT_CLEANUP_WORKSPACE: 'true'
}
});
// Test assertions
try {
assert.strictEqual(fs.existsSync(testDir), false, 'Directory should be deleted');
} catch (error) {
console.error('Test failed:', error.message);
}
});
});