Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions cxAstScan/test/_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,41 @@ describe('Task runner test', function () {
true,
"should display cleanup message: Log file not created. Task ended successfully.");
});

it('should handle results generation failure gracefully', async function () {
this.timeout(3000000);
const tp = path.join(__dirname, 'failure_generate_results.js');
const tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);
await tr.runAsync(nodeVersion);

console.log(tr.stdout);
console.log(tr.stderr);
assert.ok(tr.failed);
assert.strictEqual(tr.stdout.indexOf('Error generating the results:') >= 0, true);
});

it('should handle cleanup errors gracefully', async function () {
this.timeout(3000000);
const tp = path.join(__dirname, 'failure_cleanup.js');
const tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);
await tr.runAsync(nodeVersion);

console.log(tr.stdout);
console.log(tr.stderr);
// Even with errors, cleanup should complete successfully
assert.ok(tr.succeeded);
assert.strictEqual(tr.stdout.indexOf('Unable to delete log file.') >= 0, true);
});

it('should handle missing authentication parameters', async function () {
this.timeout(3000000);
const tp = path.join(__dirname, 'failure_auth.js');
const tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);
await tr.runAsync(nodeVersion);

console.log(tr.stdout);
console.log(tr.stderr);
assert.ok(tr.failed);
assert.strictEqual(tr.stdout.indexOf('No client ID and secret configured') >= 0, true);
});
});
17 changes: 17 additions & 0 deletions cxAstScan/test/failure_auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import tmrm = require('azure-pipelines-task-lib/mock-run');
import path = require('path');
import process from "process";

const taskPath = path.join(__dirname, '..', 'index.js');
const tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

// Setup minimal configuration without auth parameters
tmr.setInput("tenantName", process.env.CX_TENANT!);
tmr.setInput("CheckmarxService", "cxauth");
tmr.registerMockExport('getEndpointUrl', () => { return process.env.CX_BASE_URI!; });
tmr.registerMockExport('getEndpointAuthorizationParameter', () => { return ""; });

tmr.setInput("projectName", 'Test_Auth_Failure');
tmr.setInput("branchName", 'main');

tmr.run();
24 changes: 24 additions & 0 deletions cxAstScan/test/failure_cleanup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import tmrm = require('azure-pipelines-task-lib/mock-run');
import path = require('path');
import process from "process";

process.env['Build_BuildId'] = 'test_build_id';
process.env["AGENT_JOBSTATUS"] = 'Canceled';

const taskPath = path.join(__dirname, '..', 'cleanup.js');
const tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

// Setup mock environment
tmr.setInput("tenantName", process.env.CX_TENANT!);
tmr.setInput("CheckmarxService", "cxauth");
tmr.registerMockExport('getEndpointUrl', () => { return process.env.CX_BASE_URI!; });
tmr.registerMockExport('getEndpointAuthorizationParameter', (endpoint, key) => {
if (key === 'username') return process.env.CX_CLIENT_ID!
if (key === 'password') return process.env.CX_CLIENT_SECRET!
return "";
});

// Set an invalid temp directory to trigger file access errors
tmr.setVariable('Agent.TempDirectory', '/invalid/path');

Check failure on line 22 in cxAstScan/test/failure_cleanup.ts

View workflow job for this annotation

GitHub Actions / integration-tests

Property 'setVariable' does not exist on type 'TaskMockRunner'.

tmr.run();
26 changes: 26 additions & 0 deletions cxAstScan/test/failure_generate_results.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import tmrm = require('azure-pipelines-task-lib/mock-run');
import path = require('path');
import process from "process";

const taskPath = path.join(__dirname, '..', 'index.js');
const tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

// Setup mock environment
tmr.setInput("tenantName", process.env.CX_TENANT!);
tmr.setInput("CheckmarxService", "cxauth");
tmr.registerMockExport('getEndpointUrl', () => { return process.env.CX_BASE_URI!; });
tmr.registerMockExport('getEndpointAuthorizationParameter', (endpoint, key) => {
if (key === 'username') return process.env.CX_CLIENT_ID!
if (key === 'password') return process.env.CX_CLIENT_SECRET!
return "";
});

// Set inputs that will trigger the results generation path
tmr.setInput("projectName", 'Test_Results_Generation');
tmr.setInput("branchName", 'main');
tmr.setInput("additionalParams", '--scan-types sast');

// Mock the temp directory to be invalid to trigger error handling
tmr.setVariable('Agent.TempDirectory', '/invalid/path');

Check failure on line 24 in cxAstScan/test/failure_generate_results.ts

View workflow job for this annotation

GitHub Actions / integration-tests

Property 'setVariable' does not exist on type 'TaskMockRunner'.

tmr.run();
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
// ... other configurations ...
coveragePathIgnorePatterns: [
"node_modules/",
"cxAstScan/index.ts",
"cxAstScan/cleanup.ts",
"test/"
]
};
Loading
Loading