From e67925d0f7ca27f69a96527db1d8ce002382b5aa Mon Sep 17 00:00:00 2001 From: lsaudon Date: Fri, 7 Jul 2023 14:18:39 +0200 Subject: [PATCH] fix: If not find license file --- example/pubspec.yaml | 8 ++- lib/src/commands/report_command.dart | 15 +++--- test/src/commands/input.dart | 15 ++++-- test/src/commands/report_command_test.dart | 59 +++++++++++++++------- 4 files changed, 63 insertions(+), 34 deletions(-) diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 877981c..f8a34b8 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,15 +1,13 @@ name: example description: A sample command-line application. version: 1.0.0 -# repository: https://github.com/my_org/my_repo environment: sdk: ^3.0.5 -# Add regular dependencies here. dependencies: - # path: ^1.8.0 + path: ^1.8.3 dev_dependencies: - lints: ^2.0.0 - test: ^1.21.0 + lints: ^2.1.1 + test: ^1.24.4 diff --git a/lib/src/commands/report_command.dart b/lib/src/commands/report_command.dart index 65e5140..4c1e59f 100644 --- a/lib/src/commands/report_command.dart +++ b/lib/src/commands/report_command.dart @@ -72,16 +72,17 @@ class ReportCommand extends Command { .map( (final MapEntry e) => MapEntry(e.key, _fileSystem.file(e.value)), - ); + ) + .where((final MapEntry e) => e.value.existsSync()); for (final MapEntry element in collection) { - final List list = await detectLicenseInFile( + final String licenses = (await detectLicenseInFile( element.value.absolute, relativePath: element.value.path, - ); - final String licenses = - list.map((final License e) => e.spdxIdentifier).reduce( - (final String value, final String element) => '$value,$element', - ); + )) + .map((final License e) => e.spdxIdentifier) + .reduce( + (final String value, final String element) => '$value,$element', + ); _logger.info( '${element.key};$licenses;https://pub.dev/packages/${element.key}/license', ); diff --git a/test/src/commands/input.dart b/test/src/commands/input.dart index 3366ab2..bf65f38 100644 --- a/test/src/commands/input.dart +++ b/test/src/commands/input.dart @@ -1,5 +1,14 @@ -const String feAnalyzerSharedRootUri = +import 'dart:io'; + +const String feAnalyzerSharedRootUriWindows = 'file:///C:/Users/name/AppData/Local/Pub/Cache/hosted/pub.dev/_fe_analyzer_shared-62.0.0'; +const String feAnalyzerSharedRootUriMacOs = + 'file:///Users/name/.pub-cache/hosted/pub.dev/_fe_analyzer_shared-62.0.0'; + +String feAnalyzerSharedRootUri = Platform.isWindows + ? feAnalyzerSharedRootUriWindows + : feAnalyzerSharedRootUriMacOs; + const String feAnalyzerSharedLicense = ''' Copyright 2019, the Dart project authors. @@ -29,13 +38,13 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.'''; -const String packageConfigJsonContent = ''' +String packageConfigJsonContent = ''' { "configVersion": 2, "packages": [ { "name": "_fe_analyzer_shared", - "rootUri": "$feAnalyzerSharedRootUri", + "rootUri": "${Platform.isWindows ? feAnalyzerSharedRootUriWindows : feAnalyzerSharedRootUriMacOs}", "packageUri": "lib/", "languageVersion": "3.0" }, diff --git a/test/src/commands/report_command_test.dart b/test/src/commands/report_command_test.dart index 42186ef..6b5aca6 100644 --- a/test/src/commands/report_command_test.dart +++ b/test/src/commands/report_command_test.dart @@ -1,6 +1,5 @@ import 'dart:io'; -import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:license_harvest_cli/src/command_runner.dart'; import 'package:mason_logger/mason_logger.dart'; @@ -14,44 +13,66 @@ class _MockLogger extends Mock implements Logger {} void main() { group('report', () { - late FileSystem fileSystem; - late Logger logger; - late LicenseHarvestCliCommandRunner commandRunner; - - setUp(() { - fileSystem = MemoryFileSystem.test( + test('with a license file', () async { + final MemoryFileSystem fileSystem = MemoryFileSystem.test( style: Platform.isWindows ? FileSystemStyle.windows : FileSystemStyle.posix, ); - logger = _MockLogger(); - commandRunner = LicenseHarvestCliCommandRunner( + + fileSystem.file(Uri.parse(p.join(feAnalyzerSharedRootUri, 'LICENSE'))) + ..createSync(recursive: true) + ..writeAsStringSync(feAnalyzerSharedLicense); + + fileSystem.file(p.join('.dart_tool', 'package_config.json')) + ..createSync(recursive: true) + ..writeAsStringSync(packageConfigJsonContent); + + final _MockLogger logger = _MockLogger(); + final int exitCode = await LicenseHarvestCliCommandRunner( logger: logger, fileSystem: fileSystem, + ).run(['report']); + + expect(exitCode, ExitCode.success.code); + + verify(() => logger.info('name;licenses;url_license')); + verify( + () => logger.info( + '_fe_analyzer_shared;BSD-3-Clause;https://pub.dev/packages/_fe_analyzer_shared/license', + ), ); }); - test('tells a joke', () async { - fileSystem.file( - Uri.parse(p.join(feAnalyzerSharedRootUri, 'LICENSE')), - ) - ..createSync(recursive: true) - ..writeAsStringSync(feAnalyzerSharedLicense); + test('without a license file', () async { + final MemoryFileSystem fileSystem = MemoryFileSystem.test( + style: Platform.isWindows + ? FileSystemStyle.windows + : FileSystemStyle.posix, + ); + + fileSystem + .file(Uri.parse(p.join(feAnalyzerSharedRootUri))) + .createSync(recursive: true); fileSystem.file(p.join('.dart_tool', 'package_config.json')) ..createSync(recursive: true) ..writeAsStringSync(packageConfigJsonContent); - final int exitCode = await commandRunner.run(['report']); + final _MockLogger logger = _MockLogger(); + final int exitCode = await LicenseHarvestCliCommandRunner( + logger: logger, + fileSystem: fileSystem, + ).run(['report']); expect(exitCode, ExitCode.success.code); - verify(() => logger.info('name;licenses;url_license')).called(1); - verify( + verify(() => logger.info('name;licenses;url_license')); + verifyNever( () => logger.info( '_fe_analyzer_shared;BSD-3-Clause;https://pub.dev/packages/_fe_analyzer_shared/license', ), - ).called(1); + ); }); }); }