Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore empty allowed or forbidden #868

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ class PackagesCheckLicensesCommand extends Command<int> {
final forbiddenLicenses = _argResults['forbidden'] as List<String>;
final skippedPackages = _argResults['skip-packages'] as List<String>;

allowedLicenses.removeWhere((license) => license.trim().isEmpty);
forbiddenLicenses.removeWhere((license) => license.trim().isEmpty);

if (allowedLicenses.isNotEmpty && forbiddenLicenses.isNotEmpty) {
usageException(
'''Cannot specify both ${styleItalic.wrap('allowed')} and ${styleItalic.wrap('forbidden')} options.''',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ void main() {
group('packages check licenses', () {
const commandArguments = ['packages', 'check', 'licenses'];

const forbiddenArgument = '--forbidden';
const allowedArgument = '--allowed';

late Progress progress;

setUpAll(() {
Expand Down Expand Up @@ -94,10 +97,10 @@ void main() {
});

group(
'reports licenses',
'reports licenses correctly',
() {
test(
'''correctly when there is a single hosted direct dependency and license''',
'''when there is a single hosted direct dependency and license''',
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
Expand Down Expand Up @@ -128,7 +131,7 @@ void main() {
);

test(
'''correctly when there are multiple hosted direct dependency and licenses''',
'''when there are multiple hosted direct dependency and licenses''',
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
Expand Down Expand Up @@ -165,6 +168,44 @@ void main() {
expect(result, equals(ExitCode.success.code));
}),
);

test(
'''when both allowed and forbidden are specified but left empty''',
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
addTearDown(() => tempDirectory.deleteSync(recursive: true));

File(path.join(tempDirectory.path, pubspecLockBasename))
.writeAsStringSync(_validPubspecLockContent);

when(() => logger.progress(any())).thenReturn(progress);

final result = await commandRunner.run(
[
...commandArguments,
allowedArgument,
'',
forbiddenArgument,
'',
tempDirectory.path,
],
);

verify(
() => progress.update(
'Collecting licenses from 1 out of 1 package',
),
).called(1);
verify(
() => progress.complete(
'''Retrieved 1 license from 1 package of type: MIT (1).''',
),
).called(1);

expect(result, equals(ExitCode.success.code));
}),
);
},
);

Expand Down Expand Up @@ -637,8 +678,6 @@ void main() {
});

group('allowed', () {
const allowedArgument = '--allowed';

test(
'warns when a license is not recognized',
withRunner(
Expand Down Expand Up @@ -744,6 +783,50 @@ void main() {
}),
);

test(
'when a single license is not allowed and forbidden is left empty',
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
addTearDown(() => tempDirectory.deleteSync(recursive: true));

File(path.join(tempDirectory.path, pubspecLockBasename))
.writeAsStringSync(_validMultiplePubspecLockContent);

when(() => logger.progress(any())).thenReturn(progress);

const dependency1Name = 'very_good_test_runner';
when(() => pubLicense.getLicense(dependency1Name))
.thenAnswer((_) => Future.value({'MIT'}));
final license1LinkedMessage = link(
uri: pubLicenseUri(dependency1Name),
message: 'MIT',
);

const dependency2Name = 'cli_completion';
when(() => pubLicense.getLicense(dependency2Name))
.thenAnswer((_) => Future.value({'BSD'}));

await commandRunner.run(
[
...commandArguments,
allowedArgument,
'BSD',
forbiddenArgument,
'',
tempDirectory.path,
],
);

final errorMessage =
'''1 dependency has a banned license: $dependency1Name ($license1LinkedMessage).''';

verify(
() => logger.err(errorMessage),
).called(1);
}),
alestiago marked this conversation as resolved.
Show resolved Hide resolved
);

test(
'when multiple licenses are not allowed',
withRunner(
Expand Down Expand Up @@ -793,8 +876,6 @@ void main() {
});

group('forbidden', () {
const forbiddenArgument = '--forbidden';

test(
'warns when a license is not recognized',
withRunner(
Expand Down Expand Up @@ -900,6 +981,50 @@ void main() {
}),
);

test(
'when a single license is forbidden and allowed is left empty',
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
addTearDown(() => tempDirectory.deleteSync(recursive: true));

File(path.join(tempDirectory.path, pubspecLockBasename))
.writeAsStringSync(_validMultiplePubspecLockContent);

when(() => logger.progress(any())).thenReturn(progress);

const dependency1Name = 'very_good_test_runner';
when(() => pubLicense.getLicense(dependency1Name))
.thenAnswer((_) => Future.value({'MIT'}));
final license1LinkedMessage = link(
uri: pubLicenseUri(dependency1Name),
message: 'MIT',
);

const dependency2Name = 'cli_completion';
when(() => pubLicense.getLicense(dependency2Name))
.thenAnswer((_) => Future.value({'BSD'}));

await commandRunner.run(
[
...commandArguments,
allowedArgument,
'',
forbiddenArgument,
'MIT',
tempDirectory.path,
],
);

final errorMessage =
'''1 dependency has a banned license: $dependency1Name ($license1LinkedMessage).''';

verify(
() => logger.err(errorMessage),
).called(1);
}),
alestiago marked this conversation as resolved.
Show resolved Hide resolved
);

test(
'when multiple licenses are forbidden',
withRunner(
Expand Down