Skip to content

Commit

Permalink
Improve coverage (#79)
Browse files Browse the repository at this point in the history
Co-authored-by: Ilia Kebets <104737176+ilia-kebets-sonarsource@users.noreply.github.com>
  • Loading branch information
vdiez and ilia-kebets-sonarsource committed Nov 16, 2023
1 parent 08dca0e commit ca8ca24
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function getExecutableParams(params = {}) {
try {
finalUrl = new URL(fileName, baseUrl);
} catch (e) {
log(`Invalid URL "${finalUrl}". Will use default mirror "${SONAR_SCANNER_MIRROR}"`);
log(`Invalid URL "${baseUrl}". Will use default mirror "${SONAR_SCANNER_MIRROR}"`);
finalUrl = new URL(fileName, SONAR_SCANNER_MIRROR);
}

Expand Down
76 changes: 67 additions & 9 deletions test/unit/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ function pathForProject(projectFolder) {
}

describe('config', function () {
const exclusions = DEFAULT_EXCLUSIONS;

let envBackup = {};
beforeEach(function () {
envBackup = Object.assign({}, process.env);
Expand All @@ -52,7 +50,7 @@ describe('config', function () {
const expectedResult = {
'sonar.projectDescription': 'No description.',
'sonar.sources': '.',
'sonar.exclusions': exclusions,
'sonar.exclusions': DEFAULT_EXCLUSIONS,
};

assert.deepEqual(
Expand All @@ -79,7 +77,7 @@ describe('config', function () {
'sonar.token': 'my_token',
'sonar.projectDescription': 'No description.',
'sonar.sources': '.',
'sonar.exclusions': exclusions,
'sonar.exclusions': DEFAULT_EXCLUSIONS,
};

const sqParams = getScannerParams(pathForProject('fake_project_with_no_package_file'), {
Expand All @@ -96,7 +94,7 @@ describe('config', function () {
'sonar.projectDescription': 'No description.',
'sonar.sources': '.',
'sonar.tests': 'specs',
'sonar.exclusions': exclusions,
'sonar.exclusions': DEFAULT_EXCLUSIONS,
};

const sqParams = getScannerParams(pathForProject('fake_project_with_no_package_file'), {
Expand Down Expand Up @@ -154,7 +152,7 @@ describe('config', function () {
'sonar.links.scm': 'git+https://github.com/fake/project.git',
'sonar.sources': '.',
'sonar.testExecutionReportPaths': 'xunit.xml',
'sonar.exclusions': exclusions,
'sonar.exclusions': DEFAULT_EXCLUSIONS,
};

const sqParams = getScannerParams(
Expand All @@ -170,7 +168,7 @@ describe('config', function () {
'sonar.token': 'my_token',
'sonar.projectDescription': 'No description.',
'sonar.sources': '.',
'sonar.exclusions': exclusions,
'sonar.exclusions': DEFAULT_EXCLUSIONS,
};

process.env = {
Expand All @@ -193,7 +191,7 @@ describe('config', function () {
'sonar.login': 'my_token',
'sonar.projectDescription': 'No description.',
'sonar.sources': '.',
'sonar.exclusions': exclusions,
'sonar.exclusions': DEFAULT_EXCLUSIONS,
};

process.env = {
Expand Down Expand Up @@ -252,7 +250,7 @@ describe('config', function () {
SONARQUBE_SCANNER_PARAMS: JSON.stringify({
'sonar.projectDescription': 'No description.',
'sonar.sources': '.',
'sonar.exclusions': exclusions,
'sonar.exclusions': DEFAULT_EXCLUSIONS,
'sonar.host.url': 'https://sonarcloud.io',
'sonar.branch': 'dev',
}),
Expand Down Expand Up @@ -391,6 +389,66 @@ describe('config', function () {
);
});

it('should not set the http proxy if url is invalid', function () {
process.env = {
http_proxy: 'http://user:password@httpp:roxy:3128',
};
const config = getExecutableParams();
assert.notExists(config.httpOptions.httpRequestOptions);
});

it('should not set baseURL if url is invalid', function () {
const config = getExecutableParams({
baseUrl: 'http://example.com:80:80/sonarqube-repository/',
});
assert.equal(
config.downloadUrl,
new URL(
'sonar-scanner-cli-' + DEFAULT_SCANNER_VERSION + '-' + config.targetOS + '.zip',
SONAR_SCANNER_MIRROR,
),
);
});

it('should take the version from env or params', function () {
process.env.npm_config_sonar_scanner_version = '4.8.1.3023';
assert.equal(
getExecutableParams().downloadUrl,
new URL(
'sonar-scanner-cli-' + '4.8.1.3023' + '-' + findTargetOS() + '.zip',
SONAR_SCANNER_MIRROR,
),
);

process.env.SONAR_SCANNER_VERSION = '5.0.0.2966';
assert.equal(
getExecutableParams().downloadUrl,
new URL(
'sonar-scanner-cli-' + '5.0.0.2966' + '-' + findTargetOS() + '.zip',
SONAR_SCANNER_MIRROR,
),
);

assert.equal(
getExecutableParams({ version: '4.7.0.2747' }).downloadUrl,
new URL(
'sonar-scanner-cli-' + '4.7.0.2747' + '-' + findTargetOS() + '.zip',
SONAR_SCANNER_MIRROR,
),
);
});

it('should not set the scanner version if invalid', function () {
process.env.npm_config_sonar_scanner_version = '4 && rm -rf';
assert.equal(
getExecutableParams().downloadUrl,
new URL(
'sonar-scanner-cli-' + DEFAULT_SCANNER_VERSION + '-' + findTargetOS() + '.zip',
SONAR_SCANNER_MIRROR,
),
);
});

it('should consume and preserve username and password for sonar-scanner mirror server', function () {
process.env = {};
const config = getExecutableParams({
Expand Down
17 changes: 17 additions & 0 deletions test/unit/sonar-scanner-executable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ describe('sqScannerExecutable', function () {
});
});

describe('when providing an invalid CA certificate', function () {
let caPath;
beforeAll(() => {
caPath = path.join(os.tmpdir(), 'ca.pem');
fs.writeFileSync(caPath, '-----ILLEGAL CERTIFICATE-----');
});

it('should fail if the provided path is invalid', async function () {
try {
await getScannerExecutable(false, { caPath });
assert.fail('should have thrown');
} catch (e) {
assert.equal(e.message, 'Invalid CA certificate');
}
});
});

describe('local: getScannerExecutable(true)', () => {
it('should fail when the executable is not found', async () => {
assert.throws(
Expand Down

0 comments on commit ca8ca24

Please sign in to comment.