Skip to content

Commit 0670bb0

Browse files
mrgnhnt96alestiago
andauthored
refactor: Use relative path instead of absolute when running test (#920)
* feat: use relative path when printing to console for flutter test * fix: add . for current directory * feat: update variable name to be more meaningful * feat: add space between progress dots. Add check for root to display only . * refactor: remove uncessary variable declaration --------- Co-authored-by: Alejandro Santiago <dev@alestiago.com>
1 parent 0d7519b commit 0670bb0

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

lib/src/cli/flutter_cli.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ class Flutter {
153153
FlutterTestRunner testRunner = flutterTest,
154154
GeneratorBuilder buildGenerator = MasonGenerator.fromBundle,
155155
}) async {
156+
final initialCwd = cwd;
157+
156158
return _runCommand<int>(
157159
cmd: (cwd) async {
158160
final lcovPath = p.join(cwd, 'coverage', 'lcov.info');
@@ -165,14 +167,17 @@ class Flutter {
165167
void noop(String? _) {}
166168
final target = DirectoryGeneratorTarget(Directory(p.normalize(cwd)));
167169
final workingDirectory = target.dir.absolute.path;
170+
final relativePath = p.relative(workingDirectory, from: initialCwd);
171+
final path =
172+
relativePath == '.' ? '.' : '.${p.context.separator}$relativePath';
168173

169174
stdout?.call(
170-
'Running "flutter test" in ${p.dirname(workingDirectory)}...\n',
175+
'Running "flutter test" in $path ...\n',
171176
);
172177

173178
if (!Directory(p.join(target.dir.absolute.path, 'test')).existsSync()) {
174179
stdout?.call(
175-
'No test folder found in ${target.dir.absolute.path}\n',
180+
'No test folder found in $path\n',
176181
);
177182
return ExitCode.success.code;
178183
}

test/src/cli/flutter_cli_test.dart

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,8 @@ void main() {
394394
expect(
395395
stdoutLogs,
396396
equals([
397-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
398-
'No test folder found in ${tempDirectory.absolute.path}\n',
397+
'Running "flutter test" in . ...\n',
398+
'No test folder found in .\n',
399399
]),
400400
);
401401
});
@@ -431,7 +431,7 @@ void main() {
431431
expect(
432432
stdoutLogs,
433433
equals([
434-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
434+
'Running "flutter test" in . ...\n',
435435
'\x1B[2K\r00:00 ...',
436436
contains('All tests passed!'),
437437
]),
@@ -462,7 +462,7 @@ void main() {
462462
expect(
463463
stdoutLogs,
464464
equals([
465-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
465+
'Running "flutter test" in . ...\n',
466466
'\x1B[2K\r00:02 +1: CounterCubit initial state is 0',
467467
'''\x1B[2K\r00:02 +2: CounterCubit emits [1] when increment is called''',
468468
'''\x1B[2K\r00:02 +3: CounterCubit emits [-1] when decrement is called''',
@@ -504,7 +504,7 @@ void main() {
504504
expect(
505505
stdoutLogs,
506506
equals([
507-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
507+
'Running "flutter test" in . ...\n',
508508
'''\x1B[2K\r\x1B[90m00:02\x1B[0m \x1B[92m+1\x1B[0m: CounterCubit initial state is 0''',
509509
'''\x1B[2K\r\x1B[90m00:02\x1B[0m \x1B[92m+2\x1B[0m: CounterCubit emits [1] when increment is called''',
510510
'''\x1B[2K\r\x1B[90m00:02\x1B[0m \x1B[92m+3\x1B[0m: CounterCubit emits [-1] when decrement is called''',
@@ -544,7 +544,7 @@ void main() {
544544
expect(
545545
stdoutLogs,
546546
equals([
547-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
547+
'Running "flutter test" in . ...\n',
548548
'\x1B[2K\r00:11 -1: CounterCubit initial state is 0',
549549
'''\x1B[2K\r00:11 +1 -1: CounterCubit emits [1] when increment is called''',
550550
'''\x1B[2K\r00:11 +2 -1: CounterCubit emits [-1] when decrement is called''',
@@ -599,7 +599,7 @@ void main() {
599599
expect(
600600
stdoutLogs,
601601
equals([
602-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
602+
'Running "flutter test" in . ...\n',
603603
'\x1B[2K\rSkip: currently failing (see issue 1234)\n',
604604
'\x1B[2K\r(suite) ${tempDirectory.path}/test/counter/view/other_test.dart (SKIPPED)\n',
605605
'\x1B[2K\r00:00 ~1: (suite)',
@@ -846,7 +846,7 @@ void main() {
846846
expect(
847847
stdoutLogs,
848848
equals([
849-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
849+
'Running "flutter test" in . ...\n',
850850
contains('All tests passed!'),
851851
]),
852852
);
@@ -882,7 +882,7 @@ void main() {
882882
expect(
883883
stdoutLogs,
884884
equals([
885-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
885+
'Running "flutter test" in . ...\n',
886886
'Shuffling test order with --test-randomize-ordering-seed=$seed\n',
887887
contains('All tests passed!'),
888888
]),
@@ -928,7 +928,7 @@ void main() {
928928
expect(
929929
stdoutLogs,
930930
equals([
931-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
931+
'Running "flutter test" in . ...\n',
932932
contains('All tests passed!'),
933933
]),
934934
);
@@ -969,7 +969,7 @@ void main() {
969969
expect(
970970
stdoutLogs,
971971
equals([
972-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
972+
'Running "flutter test" in . ...\n',
973973
contains('All tests passed!'),
974974
]),
975975
);
@@ -1016,7 +1016,7 @@ void main() {
10161016
expect(
10171017
stdoutLogs,
10181018
equals([
1019-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
1019+
'Running "flutter test" in . ...\n',
10201020
contains('All tests passed!'),
10211021
]),
10221022
);
@@ -1061,7 +1061,7 @@ void main() {
10611061
expect(
10621062
stdoutLogs,
10631063
equals([
1064-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
1064+
'Running "flutter test" in . ...\n',
10651065
contains('All tests passed!'),
10661066
]),
10671067
);
@@ -1114,13 +1114,19 @@ void main() {
11141114
completion(equals([ExitCode.success.code, ExitCode.success.code])),
11151115
);
11161116

1117+
final nestedRelativePath = p.relative(
1118+
tempNestedDirectory.path,
1119+
from: tempDirectory.path,
1120+
);
1121+
final relativePathPrefix = '.${p.context.separator}';
1122+
11171123
expect(
11181124
stdoutLogs,
11191125
unorderedEquals([
11201126
'Running "flutter test" in '
1121-
'${p.dirname(tempNestedDirectory.path)}...\n',
1127+
'$relativePathPrefix$nestedRelativePath ...\n',
11221128
contains('All tests passed!'),
1123-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
1129+
'Running "flutter test" in . ...\n',
11241130
contains('All tests passed!'),
11251131
]),
11261132
);
@@ -1179,14 +1185,20 @@ void main() {
11791185
),
11801186
);
11811187

1188+
final nestedRelativePath = p.relative(
1189+
tempNestedDirectory.path,
1190+
from: tempDirectory.path,
1191+
);
1192+
final relativePathPrefix = '.${p.context.separator}';
1193+
11821194
expect(
11831195
stdoutLogs,
11841196
unorderedEquals([
11851197
'Running "flutter test" in '
1186-
'${p.dirname(tempDirectory.path)}...\n',
1198+
'. ...\n',
11871199
contains('All tests passed!'),
11881200
'Running "flutter test" in '
1189-
'${p.dirname(tempNestedDirectory.path)}...\n',
1201+
'$relativePathPrefix$nestedRelativePath ...\n',
11901202
contains('All tests passed!'),
11911203
]),
11921204
);
@@ -1242,7 +1254,7 @@ void main() {
12421254
expect(
12431255
stdoutLogs,
12441256
equals([
1245-
'Running "flutter test" in ${p.dirname(tempDirectory.path)}...\n',
1257+
'Running "flutter test" in . ...\n',
12461258
contains('All tests passed!'),
12471259
]),
12481260
);

0 commit comments

Comments
 (0)