Line data Source code
1 : import 'dart:io';
2 :
3 : import 'package:testainers/src/testainers_exception.dart';
4 :
5 : class TestainersConfig {
6 : final bool debug;
7 : final Duration timeout;
8 : final String runner;
9 : final Duration bootSleep;
10 : final String defaultUsername;
11 : final String defaultPassword;
12 : final String networkSuffix;
13 :
14 10 : const TestainersConfig({
15 : this.debug = false,
16 : this.timeout = const Duration(seconds: 60),
17 : this.runner = 'docker',
18 : this.bootSleep = const Duration(seconds: 10),
19 : this.defaultUsername = 'testainers',
20 : this.defaultPassword = 'testword',
21 : this.networkSuffix = '-net',
22 : });
23 :
24 9 : Future<String> runnerVersion() async {
25 27 : final ProcessResult result = await Process.run(runner, <String>[
26 : '--version',
27 18 : ]).timeout(timeout);
28 :
29 18 : if (result.exitCode != 0) {
30 0 : throw TestainersException(
31 0 : 'Runner $runner not found.',
32 0 : reason: result.stderr,
33 : );
34 : }
35 :
36 27 : return result.stdout.toString().trim();
37 : }
38 :
39 10 : Future<ProcessResult> _exec({
40 : required List<String> arguments,
41 : required String exceptionExec,
42 : }) async {
43 10 : if (debug) print('$runner ${arguments.join(' ')}');
44 :
45 10 : final ProcessResult result = await Process.run(
46 10 : runner,
47 : arguments,
48 20 : ).timeout(timeout);
49 :
50 20 : if (result.exitCode != 0) {
51 6 : throw TestainersException(exceptionExec, reason: result.stderr);
52 : }
53 :
54 : return result;
55 : }
56 :
57 10 : Future<String> exec({
58 : required List<String> arguments,
59 : required String exceptionExec,
60 : }) async {
61 10 : final ProcessResult result = await _exec(
62 : arguments: arguments,
63 : exceptionExec: exceptionExec,
64 : );
65 :
66 30 : return result.stdout.toString().trim();
67 : }
68 :
69 0 : Future<void> execAndCheck({
70 : required List<String> arguments,
71 : required String check,
72 : required String exceptionExec,
73 : required String exceptionCheck,
74 : }) async {
75 0 : final ProcessResult result = await _exec(
76 : arguments: arguments,
77 : exceptionExec: exceptionExec,
78 : );
79 :
80 0 : if (result.stdout.toString().trim() != check) {
81 0 : throw TestainersException(exceptionCheck, reason: result.stdout);
82 : }
83 : }
84 : }
|