Skip to content

Commit

Permalink
SCANNPM-38 Fully support sonar-project.properties file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
7PH committed Jun 24, 2024
1 parent 15542be commit 5d5b16d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"hpagent": "1.2.0",
"jest-sonar-reporter": "2.0.0",
"node-forge": "^1.3.1",
"properties-file": "3.5.4",
"proxy-from-env": "^1.1.0",
"semver": "7.6.0",
"slugify": "1.6.6",
Expand Down
16 changes: 3 additions & 13 deletions src/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import path from 'path';
import { getProxyForUrl } from 'proxy-from-env';
import slugify from 'slugify';
import { version } from '../package.json';
import { getProperties as getPropertiesFile } from 'properties-file';
import {
DEFAULT_SONAR_EXCLUSIONS,
ENV_TO_PROPERTY_NAME,
Expand Down Expand Up @@ -235,19 +236,8 @@ function getSonarFileProperties(projectBaseDir: string): ScannerProperties {
// Read sonar project properties file in project base dir
try {
const sonarPropertiesFile = path.join(projectBaseDir, SONAR_PROJECT_FILENAME);
const properties: ScannerProperties = {};
const data = fsExtra.readFileSync(sonarPropertiesFile).toString();
const lines = data.split(/\r?\n/);
for (const line of lines) {
const trimmedLine = line.trim();
if (trimmedLine.length === 0 || trimmedLine.startsWith('#')) {
continue;
}
const [key, value] = trimmedLine.split('=');
properties[key] = value;
}

return properties;
const data = fsExtra.readFileSync(sonarPropertiesFile);
return getPropertiesFile(data) as ScannerProperties;
} catch (error) {
log(LogLevel.DEBUG, `Failed to read ${SONAR_PROJECT_FILENAME} file: ${error}`);
return {};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# The following test parsing of properties file according to the specification
# @see https://en.wikipedia.org/wiki/.properties

# Multi-line value
sonar.exclusions=\
**/node_modules/**,\
**/docs-dist/**

# Empty value
sonar.scanner.empty.property

# Space around =
sonar.scanner.dummy.space.around.eq = value

# Whitespace at the beginning of the line
sonar.scanner.dummy.whitespace.at.beginning = \ value

# Test path
sonar.scanner.dummy.path = C:\path\to\project
24 changes: 24 additions & 0 deletions test/unit/properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,30 @@ describe('getProperties', () => {
});
});

it('should correctly parse sonar-project.properties', () => {
projectHandler.reset('fake_project_with_sonar_properties');
projectHandler.setEnvironmentVariables({});

const properties = getProperties(
{
serverUrl: 'http://localhost/sonarqube',
},
projectHandler.getStartTime(),
);

expect(properties).toEqual({
...projectHandler.getExpectedProperties(),
'sonar.host.url': 'http://localhost/sonarqube',
'sonar.scanner.apiBaseUrl': 'http://localhost/sonarqube/api/v2',
'sonar.scanner.internal.isSonarCloud': 'false',
'sonar.exclusions': '**/node_modules/**,**/docs-dist/**',
'sonar.scanner.dummy.path': 'C:path\toproject',
'sonar.scanner.dummy.space.around.eq': 'value',
'sonar.scanner.dummy.whitespace.at.beginning': ' value',
'sonar.scanner.empty.property': '',
});
});

it('does not let user override bootstrapper-only properties', () => {
projectHandler.reset('fake_project_with_sonar_properties_file');
projectHandler.setEnvironmentVariables({
Expand Down

0 comments on commit 5d5b16d

Please sign in to comment.