From 56dc83b8086e8bd941cf43893882a4e384f2af9b Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Fri, 31 Mar 2023 01:45:55 -0700 Subject: [PATCH] [cli_config] Handle paths on Windows (#58) --- .github/workflows/cli_config.yml | 39 ++++++++++++++++++- .../example/bin/cli_config_example.dart | 2 +- pkgs/cli_config/lib/src/config.dart | 9 +++-- .../test/cli_config_example_test.dart | 38 ++++++++++-------- pkgs/cli_config/test/cli_config_test.dart | 6 +-- pkgs/cli_config/test/helpers.dart | 9 +++-- 6 files changed, 74 insertions(+), 29 deletions(-) diff --git a/.github/workflows/cli_config.yml b/.github/workflows/cli_config.yml index d13cc980c..96b1aed37 100644 --- a/.github/workflows/cli_config.yml +++ b/.github/workflows/cli_config.yml @@ -16,7 +16,7 @@ on: - cron: "0 0 * * 0" # weekly jobs: - build: + build_linux: runs-on: ubuntu-latest defaults: run: @@ -53,5 +53,42 @@ jobs: uses: coverallsapp/github-action@50c33ad324a9902697adbf2f92c22cf5023eacf1 with: github-token: ${{ secrets.GITHUB_TOKEN }} + flag-name: cli_config_linux + path-to-lcov: ./pkgs/cli_config/coverage/lcov.info + if: ${{ matrix.sdk == 'stable' }} + + build_windows: + runs-on: windows-latest + defaults: + run: + working-directory: pkgs/cli_config + strategy: + matrix: + sdk: [stable, dev] # {pkgs.versions} + include: + - sdk: stable + run-tests: true + steps: + - uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c + - uses: dart-lang/setup-dart@d6a63dab3335f427404425de0fbfed4686d93c4f + with: + sdk: ${{matrix.sdk}} + + - run: dart pub get + + - run: dart test + if: ${{matrix.run-tests}} + + - name: Install coverage + run: dart pub global activate coverage + if: ${{ matrix.sdk == 'stable' }} + - name: Collect coverage + run: dart pub global run coverage:test_with_coverage + if: ${{ matrix.sdk == 'stable' }} + - name: Upload coverage + uses: coverallsapp/github-action@50c33ad324a9902697adbf2f92c22cf5023eacf1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + flag-name: cli_config_windows path-to-lcov: ./pkgs/cli_config/coverage/lcov.info if: ${{ matrix.sdk == 'stable' }} diff --git a/pkgs/cli_config/example/bin/cli_config_example.dart b/pkgs/cli_config/example/bin/cli_config_example.dart index 1b0b79363..d2b505694 100644 --- a/pkgs/cli_config/example/bin/cli_config_example.dart +++ b/pkgs/cli_config/example/bin/cli_config_example.dart @@ -8,5 +8,5 @@ Future main(List args) async { final config = await Config.fromArgs(args: args); final myPath = config.optionalPath('my_path', resolveUri: true, mustExist: false); - print(myPath?.path); + print(myPath?.toFilePath()); } diff --git a/pkgs/cli_config/lib/src/config.dart b/pkgs/cli_config/lib/src/config.dart index ed41e6e00..635178d58 100644 --- a/pkgs/cli_config/lib/src/config.dart +++ b/pkgs/cli_config/lib/src/config.dart @@ -112,9 +112,9 @@ class Config { final environmentConfig = EnvironmentParser().parse(environment); return Config._( - CliSource(cliConfig, workingDirectory), + CliSource(cliConfig, workingDirectory?.normalizePath()), EnvironmentSource(environmentConfig), - FileSource(fileConfig, fileSourceUri), + FileSource(fileConfig, fileSourceUri?.normalizePath()), ); } @@ -525,7 +525,8 @@ class Config { } void _throwIfNotExists(String key, Uri value) { - if (!value.fileSystemEntity.existsSync()) { + final fileSystemEntity = value.fileSystemEntity; + if (!fileSystemEntity.existsSync()) { throw FormatException("Path '$value' for key '$key' doesn't exist."); } } @@ -536,7 +537,7 @@ class Config { extension on Uri { FileSystemEntity get fileSystemEntity { - if (path.endsWith(Platform.pathSeparator)) { + if (path.endsWith(Platform.pathSeparator) || path.endsWith('/')) { return Directory.fromUri(this); } return File.fromUri(this); diff --git a/pkgs/cli_config/test/cli_config_example_test.dart b/pkgs/cli_config/test/cli_config_example_test.dart index adf71698c..43ed7b9eb 100644 --- a/pkgs/cli_config/test/cli_config_example_test.dart +++ b/pkgs/cli_config/test/cli_config_example_test.dart @@ -11,20 +11,21 @@ import 'helpers.dart'; void main() { test('resolve command line paths relative to working directory', () async { await inTempDir((tempUri) async { - final rootUri = Directory.current.uri; - final examplePackageUri = rootUri.resolve('example/'); - const entryPoint = 'bin/cli_config_example.dart'; + final rootUri = Directory.current.uri.normalizePath(); + final examplePackageUri = + rootUri.resolve('example${Platform.pathSeparator}'); + final entryPoint = 'bin${Platform.pathSeparator}cli_config_example.dart'; const pubSpec = 'pubspec.yaml'; for (final filename in [entryPoint, pubSpec]) { final targetUri = tempUri.resolve(filename); await File.fromUri(targetUri).create(recursive: true); await File.fromUri(examplePackageUri.resolve(filename)) - .copy(targetUri.path); + .copy(targetUri.toFilePath()); } final pubspecFile = File.fromUri(tempUri.resolve(pubSpec)); await pubspecFile.writeAsString( (await pubspecFile.readAsString()) - .replaceAll('path: ../', 'path: ${rootUri.path}'), + .replaceAll('path: ../', 'path: ${rootUri.toFilePath()}'), ); final pubGetResult = await runProcess( @@ -35,12 +36,13 @@ void main() { expect(pubGetResult.exitCode, 0); { - final commandLinePath = Uri.file('a/b/c/d.ext'); + final commandLinePath = Uri.file( + 'a${Platform.pathSeparator}b${Platform.pathSeparator}d.ext'); final result = await runProcess( executable: Uri.file(Platform.resolvedExecutable), arguments: [ - tempUri.resolve(entryPoint).path, - '-Dmy_path=${commandLinePath.path}' + tempUri.resolve(entryPoint).toFilePath(), + '-Dmy_path=${commandLinePath.toFilePath()}' ], workingDirectory: rootUri, ); @@ -50,12 +52,13 @@ void main() { } { - final commandLinePath = Uri.file('a/b/c/d.ext'); + final commandLinePath = Uri.file( + 'a${Platform.pathSeparator}b${Platform.pathSeparator}d.ext'); final result = await runProcess( executable: Uri.file(Platform.resolvedExecutable), arguments: [ - tempUri.resolve(entryPoint).path, - '-Dmy_path=${commandLinePath.path}' + tempUri.resolve(entryPoint).toFilePath(), + '-Dmy_path=${commandLinePath.toFilePath()}' ], workingDirectory: tempUri, ); @@ -64,18 +67,19 @@ void main() { expect(resolvedPath, tempUri.resolveUri(commandLinePath)); } - final pathInFile = Uri.file('a/b/c/d.ext'); + final pathInFile = + Uri.file('a${Platform.pathSeparator}b${Platform.pathSeparator}d.ext'); final configUri = tempUri.resolve('config.yaml'); await File.fromUri(configUri).writeAsString(''' -my_path: ${pathInFile.path} +my_path: ${pathInFile.toFilePath()} '''); { final result = await runProcess( executable: Uri.file(Platform.resolvedExecutable), arguments: [ - tempUri.resolve(entryPoint).path, - '--config=${configUri.path}' + tempUri.resolve(entryPoint).toFilePath(), + '--config=${configUri.toFilePath()}' ], workingDirectory: tempUri, ); @@ -88,8 +92,8 @@ my_path: ${pathInFile.path} final result = await runProcess( executable: Uri.file(Platform.resolvedExecutable), arguments: [ - tempUri.resolve(entryPoint).path, - '--config=${configUri.path}' + tempUri.resolve(entryPoint).toFilePath(), + '--config=${configUri.toFilePath()}' ], workingDirectory: rootUri, ); diff --git a/pkgs/cli_config/test/cli_config_test.dart b/pkgs/cli_config/test/cli_config_test.dart index a469b19a0..6860ad1b5 100644 --- a/pkgs/cli_config/test/cli_config_test.dart +++ b/pkgs/cli_config/test/cli_config_test.dart @@ -231,9 +231,9 @@ void main() { environment: {}, fileParsed: { 'build': { - 'out_dir': tempUri.path, - 'file': tempFileUri.path, - 'non_exist': nonExistUri.path + 'out_dir': tempUri.toFilePath(), + 'file': tempFileUri.toFilePath(), + 'non_exist': nonExistUri.toFilePath(), } }, ); diff --git a/pkgs/cli_config/test/helpers.dart b/pkgs/cli_config/test/helpers.dart index 9f2905205..d9fb40ecb 100644 --- a/pkgs/cli_config/test/helpers.dart +++ b/pkgs/cli_config/test/helpers.dart @@ -11,8 +11,11 @@ Future inTempDir( String? prefix, }) async { final tempDir = await Directory.systemTemp.createTemp(prefix); + // Deal with Windows temp folder aliases. + final tempUri = + Directory(await tempDir.resolveSymbolicLinks()).uri.normalizePath(); try { - await fun(tempDir.uri); + await fun(tempUri); } finally { if (!Platform.environment.containsKey(keepTempKey) || Platform.environment[keepTempKey]!.isEmpty) { @@ -27,9 +30,9 @@ Future runProcess({ required Uri workingDirectory, }) async { final result = await Process.run( - executable.path, + executable.toFilePath(), arguments, - workingDirectory: workingDirectory.path, + workingDirectory: workingDirectory.toFilePath(), ); if (result.exitCode != 0) { print(result.stdout);