Skip to content

Commit 34aed53

Browse files
authored
feat: ignore empty allowed or forbidden (#868)
1 parent d36819d commit 34aed53

File tree

2 files changed

+135
-7
lines changed

2 files changed

+135
-7
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ class PackagesCheckLicensesCommand extends Command<int> {
103103
final forbiddenLicenses = _argResults['forbidden'] as List<String>;
104104
final skippedPackages = _argResults['skip-packages'] as List<String>;
105105

106+
allowedLicenses.removeWhere((license) => license.trim().isEmpty);
107+
forbiddenLicenses.removeWhere((license) => license.trim().isEmpty);
108+
106109
if (allowedLicenses.isNotEmpty && forbiddenLicenses.isNotEmpty) {
107110
usageException(
108111
'''Cannot specify both ${styleItalic.wrap('allowed')} and ${styleItalic.wrap('forbidden')} options.''',

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

Lines changed: 132 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ void main() {
3636
group('packages check licenses', () {
3737
const commandArguments = ['packages', 'check', 'licenses'];
3838

39+
const forbiddenArgument = '--forbidden';
40+
const allowedArgument = '--allowed';
41+
3942
late Progress progress;
4043

4144
setUpAll(() {
@@ -94,10 +97,10 @@ void main() {
9497
});
9598

9699
group(
97-
'reports licenses',
100+
'reports licenses correctly',
98101
() {
99102
test(
100-
'''correctly when there is a single hosted direct dependency and license''',
103+
'''when there is a single hosted direct dependency and license''',
101104
withRunner(
102105
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
103106
final tempDirectory = Directory.systemTemp.createTempSync();
@@ -128,7 +131,7 @@ void main() {
128131
);
129132

130133
test(
131-
'''correctly when there are multiple hosted direct dependency and licenses''',
134+
'''when there are multiple hosted direct dependency and licenses''',
132135
withRunner(
133136
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
134137
final tempDirectory = Directory.systemTemp.createTempSync();
@@ -165,6 +168,44 @@ void main() {
165168
expect(result, equals(ExitCode.success.code));
166169
}),
167170
);
171+
172+
test(
173+
'''when both allowed and forbidden are specified but left empty''',
174+
withRunner(
175+
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
176+
final tempDirectory = Directory.systemTemp.createTempSync();
177+
addTearDown(() => tempDirectory.deleteSync(recursive: true));
178+
179+
File(path.join(tempDirectory.path, pubspecLockBasename))
180+
.writeAsStringSync(_validPubspecLockContent);
181+
182+
when(() => logger.progress(any())).thenReturn(progress);
183+
184+
final result = await commandRunner.run(
185+
[
186+
...commandArguments,
187+
allowedArgument,
188+
'',
189+
forbiddenArgument,
190+
'',
191+
tempDirectory.path,
192+
],
193+
);
194+
195+
verify(
196+
() => progress.update(
197+
'Collecting licenses from 1 out of 1 package',
198+
),
199+
).called(1);
200+
verify(
201+
() => progress.complete(
202+
'''Retrieved 1 license from 1 package of type: MIT (1).''',
203+
),
204+
).called(1);
205+
206+
expect(result, equals(ExitCode.success.code));
207+
}),
208+
);
168209
},
169210
);
170211

@@ -637,8 +678,6 @@ void main() {
637678
});
638679

639680
group('allowed', () {
640-
const allowedArgument = '--allowed';
641-
642681
test(
643682
'warns when a license is not recognized',
644683
withRunner(
@@ -744,6 +783,50 @@ void main() {
744783
}),
745784
);
746785

786+
test(
787+
'when a single license is not allowed and forbidden is left empty',
788+
withRunner(
789+
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
790+
final tempDirectory = Directory.systemTemp.createTempSync();
791+
addTearDown(() => tempDirectory.deleteSync(recursive: true));
792+
793+
File(path.join(tempDirectory.path, pubspecLockBasename))
794+
.writeAsStringSync(_validMultiplePubspecLockContent);
795+
796+
when(() => logger.progress(any())).thenReturn(progress);
797+
798+
const dependency1Name = 'very_good_test_runner';
799+
when(() => pubLicense.getLicense(dependency1Name))
800+
.thenAnswer((_) => Future.value({'MIT'}));
801+
final license1LinkedMessage = link(
802+
uri: pubLicenseUri(dependency1Name),
803+
message: 'MIT',
804+
);
805+
806+
const dependency2Name = 'cli_completion';
807+
when(() => pubLicense.getLicense(dependency2Name))
808+
.thenAnswer((_) => Future.value({'BSD'}));
809+
810+
await commandRunner.run(
811+
[
812+
...commandArguments,
813+
allowedArgument,
814+
'BSD',
815+
forbiddenArgument,
816+
'',
817+
tempDirectory.path,
818+
],
819+
);
820+
821+
final errorMessage =
822+
'''1 dependency has a banned license: $dependency1Name ($license1LinkedMessage).''';
823+
824+
verify(
825+
() => logger.err(errorMessage),
826+
).called(1);
827+
}),
828+
);
829+
747830
test(
748831
'when multiple licenses are not allowed',
749832
withRunner(
@@ -793,8 +876,6 @@ void main() {
793876
});
794877

795878
group('forbidden', () {
796-
const forbiddenArgument = '--forbidden';
797-
798879
test(
799880
'warns when a license is not recognized',
800881
withRunner(
@@ -900,6 +981,50 @@ void main() {
900981
}),
901982
);
902983

984+
test(
985+
'when a single license is forbidden and allowed is left empty',
986+
withRunner(
987+
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
988+
final tempDirectory = Directory.systemTemp.createTempSync();
989+
addTearDown(() => tempDirectory.deleteSync(recursive: true));
990+
991+
File(path.join(tempDirectory.path, pubspecLockBasename))
992+
.writeAsStringSync(_validMultiplePubspecLockContent);
993+
994+
when(() => logger.progress(any())).thenReturn(progress);
995+
996+
const dependency1Name = 'very_good_test_runner';
997+
when(() => pubLicense.getLicense(dependency1Name))
998+
.thenAnswer((_) => Future.value({'MIT'}));
999+
final license1LinkedMessage = link(
1000+
uri: pubLicenseUri(dependency1Name),
1001+
message: 'MIT',
1002+
);
1003+
1004+
const dependency2Name = 'cli_completion';
1005+
when(() => pubLicense.getLicense(dependency2Name))
1006+
.thenAnswer((_) => Future.value({'BSD'}));
1007+
1008+
await commandRunner.run(
1009+
[
1010+
...commandArguments,
1011+
allowedArgument,
1012+
'',
1013+
forbiddenArgument,
1014+
'MIT',
1015+
tempDirectory.path,
1016+
],
1017+
);
1018+
1019+
final errorMessage =
1020+
'''1 dependency has a banned license: $dependency1Name ($license1LinkedMessage).''';
1021+
1022+
verify(
1023+
() => logger.err(errorMessage),
1024+
).called(1);
1025+
}),
1026+
);
1027+
9031028
test(
9041029
'when multiple licenses are forbidden',
9051030
withRunner(

0 commit comments

Comments
 (0)