Add tests
diff --git a/pubspec.yaml b/pubspec.yaml index 0445253..8a455dc 100644 --- a/pubspec.yaml +++ b/pubspec.yaml
@@ -8,16 +8,16 @@ homepage: https://github.com/google/process_runner dependencies: + async: ^2.4.2 file: ^5.0.0 meta: ^1.1.2 path: ^1.5.1 platform: ^2.2.0 process: ^3.0.13 - async: ^2.4.2 dev_dependencies: - test: ^1.0.0 mockito: ^4.1.1 + test: ^1.0.0 environment: sdk: '>=2.3.0 <3.0.0'
diff --git a/test/src/fake_process_manager.dart b/test/src/fake_process_manager.dart index 37e56c2..ac36a80 100644 --- a/test/src/fake_process_manager.dart +++ b/test/src/fake_process_manager.dart
@@ -12,6 +12,12 @@ test_package.TypeMatcher<T> isInstanceOf<T>() => isA<T>(); +class FakeInvocationRecord { + FakeInvocationRecord(this.invocation, [this.workingDirectory]); + final List<String> invocation; + final String workingDirectory; +} + /// A mock that can be used to fake a process manager that runs commands /// and returns results. /// @@ -33,40 +39,42 @@ /// The list of results that will be sent back, organized by the command line /// that will produce them. Each command line has a list of returned stdout /// output that will be returned on each successive call. - Map<List<String>, List<ProcessResult>> _fakeResults = <List<String>, List<ProcessResult>>{}; - Map<List<String>, List<ProcessResult>> get fakeResults => _fakeResults; - set fakeResults(Map<List<String>, List<ProcessResult>> value) { - _fakeResults = <List<String>, List<ProcessResult>>{}; - for (final List<String> key in value.keys) { + Map<FakeInvocationRecord, List<ProcessResult>> _fakeResults = + <FakeInvocationRecord, List<ProcessResult>>{}; + Map<FakeInvocationRecord, List<ProcessResult>> get fakeResults => _fakeResults; + set fakeResults(Map<FakeInvocationRecord, List<ProcessResult>> value) { + _fakeResults = <FakeInvocationRecord, List<ProcessResult>>{}; + for (final FakeInvocationRecord key in value.keys) { _fakeResults[key] = (value[key] ?? <ProcessResult>[ProcessResult(0, 0, '', '')]).toList(); } } /// The list of invocations that occurred, in the order they occurred. - List<List<String>> invocations = <List<String>>[]; + List<FakeInvocationRecord> invocations = <FakeInvocationRecord>[]; /// Verify that the given command lines were called, in the given order, and /// that the parameters were in the same order. - void verifyCalls(Iterable<List<String>> calls) { + void verifyCalls(Iterable<FakeInvocationRecord> calls) { int index = 0; expect(invocations.length, equals(calls.length)); - for (final List<String> call in calls) { - expect(call, orderedEquals(invocations[index])); + for (final FakeInvocationRecord call in calls) { + expect(call.invocation, orderedEquals(invocations[index].invocation)); + expect(call.workingDirectory, equals(invocations[index].workingDirectory)); index++; } } - ProcessResult _popResult(List<String> command) { + ProcessResult _popResult(FakeInvocationRecord command) { expect(fakeResults, isNotEmpty); List<ProcessResult> foundResult; - List<String> foundCommand; - for (final List<String> fakeCommand in fakeResults.keys) { - if (fakeCommand.length != command.length) { + FakeInvocationRecord foundCommand; + for (final FakeInvocationRecord fakeCommand in fakeResults.keys) { + if (fakeCommand.invocation.length != command.invocation.length) { continue; } bool listsIdentical = true; - for (int i = 0; i < fakeCommand.length; ++i) { - if (fakeCommand[i] != command[i]) { + for (int i = 0; i < fakeCommand.invocation.length; ++i) { + if (fakeCommand.invocation[i] != command.invocation[i]) { listsIdentical = false; break; } @@ -82,21 +90,25 @@ return fakeResults[foundCommand]?.removeAt(0) ?? ProcessResult(0, 0, '', ''); } - FakeProcess _popProcess(List<String> command) => FakeProcess(_popResult(command), stdinResults); + FakeProcess _popProcess(FakeInvocationRecord command) => + FakeProcess(_popResult(command), stdinResults); - Future<Process> _nextProcess(List<String> invocation) async { - invocations.add(invocation); - return Future<Process>.value(_popProcess(invocation)); + Future<Process> _nextProcess(List<String> invocation, String workingDirectory) async { + final FakeInvocationRecord record = FakeInvocationRecord(invocation, workingDirectory); + invocations.add(record); + return Future<Process>.value(_popProcess(record)); } - ProcessResult _nextResultSync(List<String> invocation) { - invocations.add(invocation); - return _popResult(invocation); + ProcessResult _nextResultSync(List<String> invocation, String workingDirectory) { + final FakeInvocationRecord record = FakeInvocationRecord(invocation, workingDirectory); + invocations.add(record); + return _popResult(record); } - Future<ProcessResult> _nextResult(List<String> invocation) async { - invocations.add(invocation); - return Future<ProcessResult>.value(_popResult(invocation)); + Future<ProcessResult> _nextResult(List<String> invocation, String workingDirectory) async { + final FakeInvocationRecord record = FakeInvocationRecord(invocation, workingDirectory); + invocations.add(record); + return Future<ProcessResult>.value(_popResult(record)); } @override @@ -122,7 +134,7 @@ if (commandsThrow) { throw const ProcessException('failed_executable', <String>[]); } - return _nextResult(command as List<String>); + return _nextResult(command as List<String>, workingDirectory); } @override @@ -138,7 +150,7 @@ if (commandsThrow) { throw const ProcessException('failed_executable', <String>[]); } - return _nextResultSync(command as List<String>); + return _nextResultSync(command as List<String>, workingDirectory); } @override @@ -153,7 +165,7 @@ if (commandsThrow) { throw const ProcessException('failed_executable', <String>[]); } - return _nextProcess(command as List<String>); + return _nextProcess(command as List<String>, workingDirectory); } }
diff --git a/test/src/fake_process_manager_test.dart b/test/src/fake_process_manager_test.dart index b2988a2..b9440a2 100644 --- a/test/src/fake_process_manager_test.dart +++ b/test/src/fake_process_manager_test.dart
@@ -28,17 +28,18 @@ tearDown(() async {}); test('start works', () async { - final Map<List<String>, List<ProcessResult>> calls = <List<String>, List<ProcessResult>>{ - <String>['command', 'arg1', 'arg2']: <ProcessResult>[ + final Map<FakeInvocationRecord, List<ProcessResult>> calls = + <FakeInvocationRecord, List<ProcessResult>>{ + FakeInvocationRecord(<String>['command', 'arg1', 'arg2']): <ProcessResult>[ ProcessResult(0, 0, 'output1', ''), ], - <String>['command2', 'arg1', 'arg2']: <ProcessResult>[ + FakeInvocationRecord(<String>['command2', 'arg1', 'arg2']): <ProcessResult>[ ProcessResult(0, 0, 'output2', ''), ], }; processManager.fakeResults = calls; - for (final List<String> key in calls.keys) { - final Process process = await processManager.start(key); + for (final FakeInvocationRecord key in calls.keys) { + final Process process = await processManager.start(key.invocation); String output = ''; process.stdout.listen((List<int> item) { output += utf8.decode(item); @@ -50,51 +51,54 @@ }); test('run works', () async { - final Map<List<String>, List<ProcessResult>> calls = <List<String>, List<ProcessResult>>{ - <String>['command', 'arg1', 'arg2']: <ProcessResult>[ + final Map<FakeInvocationRecord, List<ProcessResult>> calls = + <FakeInvocationRecord, List<ProcessResult>>{ + FakeInvocationRecord(<String>['command', 'arg1', 'arg2']): <ProcessResult>[ ProcessResult(0, 0, 'output1', ''), ], - <String>['command2', 'arg1', 'arg2']: <ProcessResult>[ + FakeInvocationRecord(<String>['command2', 'arg1', 'arg2']): <ProcessResult>[ ProcessResult(0, 0, 'output2', ''), ], }; processManager.fakeResults = calls; - for (final List<String> key in calls.keys) { - final ProcessResult result = await processManager.run(key); + for (final FakeInvocationRecord key in calls.keys) { + final ProcessResult result = await processManager.run(key.invocation); expect(result.stdout, equals((calls[key] ?? <ProcessResult>[])[0].stdout)); } processManager.verifyCalls(calls.keys.toList()); }); test('runSync works', () async { - final Map<List<String>, List<ProcessResult>> calls = <List<String>, List<ProcessResult>>{ - <String>['command', 'arg1', 'arg2']: <ProcessResult>[ + final Map<FakeInvocationRecord, List<ProcessResult>> calls = + <FakeInvocationRecord, List<ProcessResult>>{ + FakeInvocationRecord(<String>['command', 'arg1', 'arg2']): <ProcessResult>[ ProcessResult(0, 0, 'output1', ''), ], - <String>['command2', 'arg1', 'arg2']: <ProcessResult>[ + FakeInvocationRecord(<String>['command2', 'arg1', 'arg2']): <ProcessResult>[ ProcessResult(0, 0, 'output2', ''), ], }; processManager.fakeResults = calls; - for (final List<String> key in calls.keys) { - final ProcessResult result = processManager.runSync(key); + for (final FakeInvocationRecord key in calls.keys) { + final ProcessResult result = processManager.runSync(key.invocation); expect(result.stdout, equals((calls[key] ?? <ProcessResult>[])[0].stdout)); } processManager.verifyCalls(calls.keys.toList()); }); test('captures stdin', () async { - final Map<List<String>, List<ProcessResult>> calls = <List<String>, List<ProcessResult>>{ - <String>['command', 'arg1', 'arg2']: <ProcessResult>[ + final Map<FakeInvocationRecord, List<ProcessResult>> calls = + <FakeInvocationRecord, List<ProcessResult>>{ + FakeInvocationRecord(<String>['command', 'arg1', 'arg2']): <ProcessResult>[ ProcessResult(0, 0, 'output1', ''), ], - <String>['command2', 'arg1', 'arg2']: <ProcessResult>[ + FakeInvocationRecord(<String>['command2', 'arg1', 'arg2']): <ProcessResult>[ ProcessResult(0, 0, 'output2', ''), ], }; processManager.fakeResults = calls; - for (final List<String> key in calls.keys) { - final Process process = await processManager.start(key); + for (final FakeInvocationRecord key in calls.keys) { + final Process process = await processManager.start(key.invocation); String output = ''; process.stdout.listen((List<int> item) { output += utf8.decode(item);
diff --git a/test/src/process_pool_test.dart b/test/src/process_pool_test.dart index 08339a5..c73b3a8 100644 --- a/test/src/process_pool_test.dart +++ b/test/src/process_pool_test.dart
@@ -10,22 +10,26 @@ import 'fake_process_manager.dart'; void main() { - FakeProcessManager fakeProcessManager = FakeProcessManager((String value) {}); - ProcessRunner processRunner = ProcessRunner(processManager: fakeProcessManager); - ProcessPool processPool = ProcessPool(processRunner: processRunner); + FakeProcessManager fakeProcessManager; + ProcessRunner processRunner; + ProcessPool processPool; setUp(() { fakeProcessManager = FakeProcessManager((String value) {}); - processRunner = ProcessRunner(processManager: fakeProcessManager); + processRunner = ProcessRunner( + processManager: fakeProcessManager, + defaultWorkingDirectory: Directory('/tmp/foo'), + ); processPool = ProcessPool(processRunner: processRunner, printReport: null); }); tearDown(() {}); - group('Ouput Capture', () { + group('Output Capture', () { test('startWorkers works', () async { - final Map<List<String>, List<ProcessResult>> calls = <List<String>, List<ProcessResult>>{ - <String>['command', 'arg1', 'arg2']: <ProcessResult>[ + final Map<FakeInvocationRecord, List<ProcessResult>> calls = + <FakeInvocationRecord, List<ProcessResult>>{ + FakeInvocationRecord(<String>['command', 'arg1', 'arg2'], '/tmp/foo'): <ProcessResult>[ ProcessResult(0, 0, 'output1', ''), ], }; @@ -37,8 +41,9 @@ fakeProcessManager.verifyCalls(calls.keys); }); test('runToCompletion works', () async { - final Map<List<String>, List<ProcessResult>> calls = <List<String>, List<ProcessResult>>{ - <String>['command', 'arg1', 'arg2']: <ProcessResult>[ + final Map<FakeInvocationRecord, List<ProcessResult>> calls = + <FakeInvocationRecord, List<ProcessResult>>{ + FakeInvocationRecord(<String>['command', 'arg1', 'arg2'], '/tmp/foo'): <ProcessResult>[ ProcessResult(0, 0, 'output1', ''), ], }; @@ -50,8 +55,9 @@ fakeProcessManager.verifyCalls(calls.keys); }); test('failed tests report results', () async { - final Map<List<String>, List<ProcessResult>> calls = <List<String>, List<ProcessResult>>{ - <String>['command', 'arg1', 'arg2']: <ProcessResult>[ + final Map<FakeInvocationRecord, List<ProcessResult>> calls = + <FakeInvocationRecord, List<ProcessResult>>{ + FakeInvocationRecord(<String>['command', 'arg1', 'arg2'], '/tmp/foo'): <ProcessResult>[ ProcessResult(0, -1, 'output1', 'stderr1'), ], }; @@ -69,8 +75,9 @@ fakeProcessManager = FakeProcessManager((String value) {}, commandsThrow: true); processRunner = ProcessRunner(processManager: fakeProcessManager); processPool = ProcessPool(processRunner: processRunner, printReport: null); - final Map<List<String>, List<ProcessResult>> calls = <List<String>, List<ProcessResult>>{ - <String>['command', 'arg1', 'arg2']: <ProcessResult>[ + final Map<FakeInvocationRecord, List<ProcessResult>> calls = + <FakeInvocationRecord, List<ProcessResult>>{ + FakeInvocationRecord(<String>['command', 'arg1', 'arg2'], '/tmp/foo'): <ProcessResult>[ ProcessResult(0, -1, 'output1', 'stderr1'), ], };
diff --git a/test/src/process_runner_test.dart b/test/src/process_runner_test.dart index 6d7ea1e..82992f2 100644 --- a/test/src/process_runner_test.dart +++ b/test/src/process_runner_test.dart
@@ -16,53 +16,60 @@ setUp(() { fakeProcessManager = FakeProcessManager((String value) {}); - processRunner = ProcessRunner(processManager: fakeProcessManager); + processRunner = ProcessRunner( + processManager: fakeProcessManager, defaultWorkingDirectory: Directory('/tmp/foo')); }); tearDown(() {}); group('Ouput Capture', () { test('runProcess works', () async { - final Map<List<String>, List<ProcessResult>> calls = <List<String>, List<ProcessResult>>{ - <String>['command', 'arg1', 'arg2']: <ProcessResult>[ + final Map<FakeInvocationRecord, List<ProcessResult>> calls = + <FakeInvocationRecord, List<ProcessResult>>{ + FakeInvocationRecord(<String>['command', 'arg1', 'arg2'], '/tmp/foo'): <ProcessResult>[ ProcessResult(0, 0, 'output1', ''), ], }; fakeProcessManager.fakeResults = calls; - await processRunner.runProcess(calls.keys.first); + await processRunner.runProcess(calls.keys.first.invocation); fakeProcessManager.verifyCalls(calls.keys); }); test('runProcess returns correct output', () async { - final Map<List<String>, List<ProcessResult>> calls = <List<String>, List<ProcessResult>>{ - <String>['command', 'arg1', 'arg2']: <ProcessResult>[ + final Map<FakeInvocationRecord, List<ProcessResult>> calls = + <FakeInvocationRecord, List<ProcessResult>>{ + FakeInvocationRecord(<String>['command', 'arg1', 'arg2'], '/tmp/foo'): <ProcessResult>[ ProcessResult(0, 0, 'output1', 'stderr1'), ], }; fakeProcessManager.fakeResults = calls; - final ProcessRunnerResult result = await processRunner.runProcess(calls.keys.first); + final ProcessRunnerResult result = + await processRunner.runProcess(calls.keys.first.invocation); fakeProcessManager.verifyCalls(calls.keys); expect(result.stdout, equals('output1')); expect(result.stderr, equals('stderr1')); expect(result.output, equals('output1stderr1')); }); test('runProcess fails properly', () async { - final Map<List<String>, List<ProcessResult>> calls = <List<String>, List<ProcessResult>>{ - <String>['command', 'arg1', 'arg2']: <ProcessResult>[ + final Map<FakeInvocationRecord, List<ProcessResult>> calls = + <FakeInvocationRecord, List<ProcessResult>>{ + FakeInvocationRecord(<String>['command', 'arg1', 'arg2'], ''): <ProcessResult>[ ProcessResult(0, -1, 'output1', 'stderr1'), ], }; fakeProcessManager.fakeResults = calls; - await expectLater(() => processRunner.runProcess(calls.keys.first), throwsException); + await expectLater( + () => processRunner.runProcess(calls.keys.first.invocation), throwsException); }); test('runProcess returns the failed results properly', () async { - final Map<List<String>, List<ProcessResult>> calls = <List<String>, List<ProcessResult>>{ - <String>['command', 'arg1', 'arg2']: <ProcessResult>[ + final Map<FakeInvocationRecord, List<ProcessResult>> calls = + <FakeInvocationRecord, List<ProcessResult>>{ + FakeInvocationRecord(<String>['command', 'arg1', 'arg2'], '/tmp/foo'): <ProcessResult>[ ProcessResult(0, -1, 'output1', 'stderr1'), ], }; fakeProcessManager.fakeResults = calls; final ProcessRunnerResult result = - await processRunner.runProcess(calls.keys.first, failOk: true); + await processRunner.runProcess(calls.keys.first.invocation, failOk: true); expect(result.stdout, equals('output1')); expect(result.stderr, equals('stderr1')); expect(result.output, equals('output1stderr1'));