Skip to content

Commit 1eef4cd

Browse files
vdiezclaude
andcommitted
Refactor DI to use parameter defaults
Move default values for dependency injection from inside function bodies to parameter destructuring. This makes it clearer which defaults are used when callers don't provide specific dependencies. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 1ce0457 commit 1eef4cd

File tree

6 files changed

+23
-42
lines changed

6 files changed

+23
-42
lines changed

src/file.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,15 @@ const defaultFileDeps: FileDeps = {
4848
export async function getCacheFileLocation(
4949
properties: ScannerProperties,
5050
{ checksum, filename, alias }: CacheFileData,
51-
deps: Partial<FileDeps> = {},
51+
fsDeps: FileDeps = defaultFileDeps,
5252
) {
53-
const fsDeps = { ...defaultFileDeps, ...deps };
5453
const filePath = path.join(getParentCacheDirectory(properties), checksum, filename);
5554
if (fsDeps.existsSync(filePath)) {
5655
log(LogLevel.DEBUG, alias, 'version found in cache:', filename);
5756

5857
// validate cache
5958
try {
60-
await validateChecksum(filePath, checksum, deps);
59+
await validateChecksum(filePath, checksum, fsDeps);
6160
} catch (error) {
6261
await fsDeps.remove(filePath);
6362
throw error;
@@ -73,9 +72,8 @@ export async function getCacheFileLocation(
7372
export async function extractArchive(
7473
fromPath: string,
7574
toPath: string,
76-
deps: Partial<FileDeps> = {},
75+
fsDeps: FileDeps = defaultFileDeps,
7776
) {
78-
const fsDeps = { ...defaultFileDeps, ...deps };
7977
log(LogLevel.DEBUG, `Extracting ${fromPath} to ${toPath}`);
8078
if (fromPath.endsWith('.tar.gz')) {
8179
const tarFilePath = fromPath;
@@ -116,8 +114,7 @@ export async function extractArchive(
116114
}
117115
}
118116

119-
async function generateChecksum(filepath: string, deps: Partial<FileDeps> = {}) {
120-
const fsDeps = { ...defaultFileDeps, ...deps };
117+
async function generateChecksum(filepath: string, fsDeps: FileDeps = defaultFileDeps) {
121118
return new Promise((resolve, reject) => {
122119
fsDeps.readFile(filepath, (err, data) => {
123120
if (err) {
@@ -132,11 +129,11 @@ async function generateChecksum(filepath: string, deps: Partial<FileDeps> = {})
132129
export async function validateChecksum(
133130
filePath: string,
134131
expectedChecksum: string,
135-
deps: Partial<FileDeps> = {},
132+
fsDeps: FileDeps = defaultFileDeps,
136133
) {
137134
if (expectedChecksum) {
138135
log(LogLevel.DEBUG, `Verifying checksum ${expectedChecksum}`);
139-
const checksum = await generateChecksum(filePath, deps);
136+
const checksum = await generateChecksum(filePath, fsDeps);
140137

141138
log(LogLevel.DEBUG, `Checksum Value: ${checksum}`);
142139
if (checksum !== expectedChecksum) {
@@ -152,9 +149,8 @@ export async function validateChecksum(
152149
export async function getCacheDirectories(
153150
properties: ScannerProperties,
154151
{ checksum, filename }: CacheFileData,
155-
deps: Partial<FileDeps> = {},
152+
fsDeps: FileDeps = defaultFileDeps,
156153
) {
157-
const fsDeps = { ...defaultFileDeps, ...deps };
158154
const archivePath = path.join(getParentCacheDirectory(properties), checksum, filename);
159155
const unarchivePath = path.join(
160156
getParentCacheDirectory(properties),

src/java.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ export interface JavaDeps {
6464

6565
export async function fetchServerVersion(
6666
properties: ScannerProperties,
67-
deps: JavaDeps = {},
67+
{ fetchFn = fetch }: JavaDeps = {},
6868
): Promise<SemVer> {
69-
const { fetchFn = fetch } = deps;
70-
7169
let version: SemVer | null = null;
7270
try {
7371
// Try and fetch the new version endpoint first
@@ -110,7 +108,7 @@ export async function fetchServerVersion(
110108

111109
export async function serverSupportsJREProvisioning(
112110
properties: ScannerProperties,
113-
deps: JavaDeps = {},
111+
{ fetchFn = fetch }: JavaDeps = {},
114112
): Promise<boolean> {
115113
if (properties[ScannerProperty.SonarScannerInternalIsSonarCloud] === 'true') {
116114
return true;
@@ -120,7 +118,7 @@ export async function serverSupportsJREProvisioning(
120118
log(LogLevel.DEBUG, 'Detecting SonarQube server version');
121119
const SQServerInfo =
122120
semver.coerce(properties[ScannerProperty.SonarScannerInternalSqVersion]) ??
123-
(await fetchServerVersion(properties, deps));
121+
(await fetchServerVersion(properties, { fetchFn }));
124122
log(LogLevel.INFO, 'SonarQube server version:', SQServerInfo.version);
125123

126124
const supports = semver.satisfies(SQServerInfo, `>=${SONARQUBE_JRE_PROVISIONING_MIN_VERSION}`);
@@ -130,18 +128,16 @@ export async function serverSupportsJREProvisioning(
130128

131129
export async function fetchJRE(
132130
properties: ScannerProperties,
133-
deps: JavaDeps = {},
134-
): Promise<string> {
135-
const {
131+
{
136132
fsDeps = defaultFsDeps,
137133
fetchFn = fetch,
138134
downloadFn = download,
139135
getCacheFileLocationFn = getCacheFileLocation,
140136
getCacheDirectoriesFn = getCacheDirectories,
141137
validateChecksumFn = validateChecksum,
142138
extractArchiveFn = extractArchive,
143-
} = deps;
144-
139+
}: JavaDeps = {},
140+
): Promise<string> {
145141
log(LogLevel.DEBUG, 'Detecting latest version of JRE');
146142
const jreMetaData = await fetchLatestSupportedJRE(properties, fetchFn);
147143
log(LogLevel.DEBUG, 'Latest Supported JRE: ', jreMetaData);

src/process.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ export interface ProcessModuleDeps {
5555
*/
5656
export async function locateExecutableFromPath(
5757
executable: string,
58-
deps: ProcessModuleDeps = {},
58+
{ processDeps = defaultProcessDeps, execAsyncFn = execAsync }: ProcessModuleDeps = {},
5959
): Promise<string | null> {
60-
const { processDeps = defaultProcessDeps, execAsyncFn = execAsync } = deps;
6160
try {
6261
log(LogLevel.INFO, `Trying to find ${executable}`);
6362
const child = await execAsyncFn(

src/properties.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,8 @@ export function getProperties(
524524
scanOptions: ScanOptions,
525525
startTimestampMs: number,
526526
cliArgs?: CliArgs,
527-
deps: PropertiesDeps = {},
527+
{ fsDeps = defaultFsDeps, processDeps = defaultProcessDeps }: PropertiesDeps = {},
528528
): ScannerProperties {
529-
const { fsDeps = defaultFsDeps, processDeps = defaultProcessDeps } = deps;
530-
531529
const envProperties = getEnvironmentProperties(processDeps);
532530
const scanOptionsProperties = getScanOptionsProperties(scanOptions);
533531
const cliProperties = getCommandLineProperties(cliArgs);

src/scanner-cli.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,13 @@ function getScannerCliUrl(
119119

120120
export async function downloadScannerCli(
121121
properties: ScannerProperties,
122-
deps: ScannerCliDeps = {},
123-
): Promise<string> {
124-
const {
122+
{
125123
fsDeps = defaultFsDeps,
126124
processDeps = defaultProcessDeps,
127125
downloadFn = download,
128126
extractArchiveFn = extractArchive,
129-
} = deps;
130-
127+
}: ScannerCliDeps = {},
128+
): Promise<string> {
131129
const versionStr = properties[ScannerProperty.SonarScannerCliVersion] ?? SCANNER_CLI_VERSION;
132130
const version = semver.coerce(versionStr);
133131
if (!version) {
@@ -181,10 +179,8 @@ export async function runScannerCli(
181179
scanOptions: ScanOptions,
182180
properties: ScannerProperties,
183181
binPath: string,
184-
deps: ScannerCliDeps = {},
182+
{ processDeps = defaultProcessDeps, spawnFn = defaultSpawn }: ScannerCliDeps = {},
185183
) {
186-
const { processDeps = defaultProcessDeps, spawnFn = defaultSpawn } = deps;
187-
188184
log(LogLevel.INFO, 'Starting analysis');
189185
// We filter out env properties that are passed to the scanner
190186
// otherwise, they would supersede the properties passed to the scanner through SONARQUBE_SCANNER_PARAMS

src/scanner-engine.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,15 @@ export interface ScannerEngineDeps {
6464

6565
export async function fetchScannerEngine(
6666
properties: ScannerProperties,
67-
deps: ScannerEngineDeps = {},
68-
) {
69-
const {
67+
{
7068
fsDeps = defaultFsDeps,
7169
fetchFn = fetch,
7270
downloadFn = download,
7371
getCacheFileLocationFn = getCacheFileLocation,
7472
getCacheDirectoriesFn = getCacheDirectories,
7573
validateChecksumFn = validateChecksum,
76-
} = deps;
77-
74+
}: ScannerEngineDeps = {},
75+
) {
7876
log(LogLevel.DEBUG, `Detecting latest version of ${SONAR_SCANNER_ALIAS}`);
7977
const { data } = await fetchFn<AnalysisEngineResponseType>({
8078
url: API_V2_SCANNER_ENGINE_ENDPOINT,
@@ -136,10 +134,8 @@ export function runScannerEngine(
136134
scannerEnginePath: string,
137135
scanOptions: ScanOptions,
138136
properties: ScannerProperties,
139-
deps: ScannerEngineDeps = {},
137+
{ fsDeps = defaultFsDeps, spawnFn = defaultSpawn }: ScannerEngineDeps = {},
140138
) {
141-
const { fsDeps = defaultFsDeps, spawnFn = defaultSpawn } = deps;
142-
143139
log(LogLevel.DEBUG, `Running the ${SONAR_SCANNER_ALIAS}`);
144140

145141
// The scanner engine expects a JSON object of properties attached to a key name "scannerProperties"

0 commit comments

Comments
 (0)