Skip to content

Commit 481c75b

Browse files
committed
fix: report malformed licences as unknown
1 parent 23e9dae commit 481c75b

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

lib/src/commands/packages/commands/check/commands/licenses.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ class PackagesCheckLicensesCommand extends Command<int> {
286286
// ignore: invalid_use_of_visible_for_testing_member
287287
.map((match) => match.license.identifier)
288288
.toSet();
289-
licenses[dependencyName] = rawLicense;
289+
licenses[dependencyName] = {
290+
...rawLicense,
291+
// If there are no matches, we add the unknown license
292+
if (rawLicense.isEmpty) SpdxLicense.$unknown.value,
293+
};
290294
}
291295

292296
late final _BannedDependencyLicenseMap? bannedDependencies;

test/src/commands/packages/commands/check/commands/licenses_test.dart

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,97 @@ void main() {
319319
expect(result, equals(ExitCode.success.code));
320320
}),
321321
);
322+
323+
test(
324+
'unknown when non-standard license file is found',
325+
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
326+
File(
327+
path.join(tempDirectory.path, pubspecLockBasename),
328+
).writeAsStringSync(_validPubspecLockContent);
329+
330+
when(
331+
() => packageConfig.packages,
332+
).thenReturn([veryGoodTestRunnerConfigPackage]);
333+
final licenseFilePath = path.join(
334+
tempDirectory.path,
335+
veryGoodTestRunnerConfigPackage.name,
336+
'LICENSE',
337+
);
338+
File(licenseFilePath).writeAsStringSync('''
339+
Licensed under the Apache License, Version 2.0 (the "License"); you
340+
may not use this file except in compliance with the License. You may
341+
obtain a copy of the License at
342+
343+
http://www.apache.org/licenses/LICENSE-2.0
344+
345+
Unless required by applicable law or agreed to in writing, software
346+
distributed under the License is distributed on an "AS IS" BASIS,
347+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
348+
implied. See the License for the specific language governing permissions
349+
and limitations under the License.''');
350+
351+
when(() => logger.progress(any())).thenReturn(progress);
352+
when(() => detectorResult.matches).thenReturn([]);
353+
354+
final result = await commandRunner.run(
355+
[...commandArguments, tempDirectory.path],
356+
);
357+
358+
verify(
359+
() => progress.update(
360+
'Collecting licenses from 1 out of 1 package',
361+
),
362+
).called(1);
363+
verify(
364+
() => progress.complete(
365+
'''Retrieved 1 license from 1 package of type: unknown (1).''',
366+
),
367+
).called(1);
368+
369+
expect(result, equals(ExitCode.success.code));
370+
}),
371+
);
372+
373+
test(
374+
'unknown when invalid license file is found',
375+
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
376+
File(
377+
path.join(tempDirectory.path, pubspecLockBasename),
378+
).writeAsStringSync(_validPubspecLockContent);
379+
380+
when(
381+
() => packageConfig.packages,
382+
).thenReturn([veryGoodTestRunnerConfigPackage]);
383+
final licenseFilePath = path.join(
384+
tempDirectory.path,
385+
veryGoodTestRunnerConfigPackage.name,
386+
'LICENSE',
387+
);
388+
File(
389+
licenseFilePath,
390+
).writeAsStringSync('This is an invalid license file.');
391+
392+
when(() => logger.progress(any())).thenReturn(progress);
393+
when(() => detectorResult.matches).thenReturn([]);
394+
395+
final result = await commandRunner.run(
396+
[...commandArguments, tempDirectory.path],
397+
);
398+
399+
verify(
400+
() => progress.update(
401+
'Collecting licenses from 1 out of 1 package',
402+
),
403+
).called(1);
404+
verify(
405+
() => progress.complete(
406+
'''Retrieved 1 license from 1 package of type: unknown (1).''',
407+
),
408+
).called(1);
409+
410+
expect(result, equals(ExitCode.success.code));
411+
}),
412+
);
322413
},
323414
);
324415

0 commit comments

Comments
 (0)