Skip to content

Commit

Permalink
[cli_config] Handle paths on Windows (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes authored Mar 31, 2023
1 parent 5994406 commit 56dc83b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 29 deletions.
39 changes: 38 additions & 1 deletion .github/workflows/cli_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:
- cron: "0 0 * * 0" # weekly

jobs:
build:
build_linux:
runs-on: ubuntu-latest
defaults:
run:
Expand Down Expand Up @@ -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' }}
2 changes: 1 addition & 1 deletion pkgs/cli_config/example/bin/cli_config_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ Future<void> main(List<String> 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());
}
9 changes: 5 additions & 4 deletions pkgs/cli_config/lib/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
);
}

Expand Down Expand Up @@ -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.");
}
}
Expand All @@ -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);
Expand Down
38 changes: 21 additions & 17 deletions pkgs/cli_config/test/cli_config_example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand Down
6 changes: 3 additions & 3 deletions pkgs/cli_config/test/cli_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
},
);
Expand Down
9 changes: 6 additions & 3 deletions pkgs/cli_config/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ Future<void> 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) {
Expand All @@ -27,9 +30,9 @@ Future<ProcessResult> 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);
Expand Down

0 comments on commit 56dc83b

Please sign in to comment.