-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.dart
More file actions
96 lines (80 loc) · 2.41 KB
/
utils.dart
File metadata and controls
96 lines (80 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import 'dart:io';
import 'package:example/src/command_runner.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
class MockStdout extends Mock implements Stdout {}
Matcher suggests(Map<String, String?> suggestions, {int? whenCursorIsAt}) =>
CliCompletionMatcher(
suggestions,
cursorIndex: whenCursorIsAt,
);
class CliCompletionMatcher extends CustomMatcher {
CliCompletionMatcher(
Map<String, String?> suggestions, {
this.cursorIndex,
}) : super(
'Completes with the expected suggestions',
'suggestions',
completion(suggestions),
);
final int? cursorIndex;
@override
Object? featureValueOf(dynamic line) {
if (line is! String) {
throw ArgumentError.value(line, 'line', 'must be a String');
}
return runCompletionCommand(line, cursorIndex: cursorIndex);
}
}
/// Simulate the shell behavior of completing a command line.
Map<String, String> prepareEnvForLineInput(String line, {int? cursorIndex}) {
final cpoint = cursorIndex ?? line.length;
var cword = 0;
line.split(' ').fold(0, (value, element) {
final total = value + 1 + element.length;
if (total < cpoint) {
cword++;
return total;
}
return value;
});
return {
'COMP_LINE': line,
'COMP_POINT': '$cpoint',
'COMP_CWORD': '$cword',
};
}
Future<Map<String, String?>> runCompletionCommand(
String line, {
int? cursorIndex,
}) async {
final map = <String, String?>{};
final stdout = MockStdout();
when(() {
stdout.writeln(any());
}).thenAnswer((invocation) {
// Simulate the shell behavior of interpreting the output of the completion.
final line = invocation.positionalArguments.first as String;
// A regex that finds all colons, except the ones preceded by backslash
final res = line.split(RegExp(r'(?<!\\):'));
final description = res.length > 1 ? res[1] : null;
map[res.first] = description;
});
await IOOverrides.runZoned(
stdout: () => stdout,
() async {
final commandRunner = ExampleCommandRunner()
..environmentOverride = {
'SHELL': '/foo/bar/zsh',
...prepareEnvForLineInput(line, cursorIndex: cursorIndex),
};
await commandRunner.run(['completion']);
},
);
return map;
}
extension CompletionUtils on Map<String, String?> {
Map<String, String?> except(String key) {
return Map.from(this)..remove(key);
}
}