Skip to content

Commit d30f59e

Browse files
authored
fix: add system shell identification by heuristics (#31)
1 parent 000486e commit d30f59e

File tree

2 files changed

+72
-38
lines changed

2 files changed

+72
-38
lines changed

lib/src/system_shell.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@ enum SystemShell {
2121
}) {
2222
final environment = environmentOverride ?? Platform.environment;
2323

24-
// TODO(renancaraujo): this detects the "login shell", which can be
25-
// different from the actual shell.
24+
// Heuristic: if ZSH_NAME is set, must be zsh
25+
final isZSH = environment['ZSH_NAME'] != null;
26+
if (isZSH) {
27+
return SystemShell.zsh;
28+
}
29+
30+
// Heuristic: if BASH is set, must be bash
31+
final isBash = environment['BASH'] != null;
32+
if (isBash) {
33+
return SystemShell.bash;
34+
}
35+
2636
final envShell = environment['SHELL'];
2737
if (envShell == null || envShell.isEmpty) return null;
2838

test/src/system_shell_test.dart

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,93 @@ import 'package:test/test.dart';
33

44
void main() {
55
group('SystemShell', () {
6-
group('fromCurrentShell', () {
6+
group('current', () {
77
test('instantiated without env', () {
88
expect(
99
SystemShell.current,
1010
returnsNormally,
1111
);
1212
});
1313

14-
test('identifies zsh', () {
15-
final result = SystemShell.current(
16-
environmentOverride: {
17-
'SHELL': '/foo/bar/zsh',
18-
},
19-
);
20-
21-
expect(result, SystemShell.zsh);
22-
});
23-
24-
test('identifies bash shell', () {
25-
final result = SystemShell.current(
26-
environmentOverride: {
27-
'SHELL': '/foo/bar/bash',
28-
},
29-
);
14+
group('Heuristics', () {
15+
test('identifies zsh', () {
16+
final result = SystemShell.current(
17+
environmentOverride: {
18+
'ZSH_NAME': 'zsh',
19+
},
20+
);
3021

31-
expect(result, SystemShell.bash);
22+
expect(result, SystemShell.zsh);
23+
});
3224

33-
final resultWindows = SystemShell.current(
34-
environmentOverride: {
35-
'SHELL': r'c:\foo\bar\bash.exe',
36-
},
37-
);
25+
test('identifies bash', () {
26+
final result = SystemShell.current(
27+
environmentOverride: {
28+
'BASH': '/bin/bash',
29+
},
30+
);
3831

39-
expect(resultWindows, SystemShell.bash);
32+
expect(result, SystemShell.bash);
33+
});
4034
});
4135

42-
group('identifies no shell', () {
43-
test('for no shell env', () {
36+
group(r'When checking $SHELL', () {
37+
test('identifies zsh', () {
4438
final result = SystemShell.current(
45-
environmentOverride: {},
39+
environmentOverride: {
40+
'SHELL': '/foo/bar/zsh',
41+
},
4642
);
4743

48-
expect(result, null);
44+
expect(result, SystemShell.zsh);
4945
});
5046

51-
test('for empty shell env', () {
47+
test('identifies bash shell', () {
5248
final result = SystemShell.current(
5349
environmentOverride: {
54-
'SHELL': '',
50+
'SHELL': '/foo/bar/bash',
5551
},
5652
);
5753

58-
expect(result, null);
59-
});
54+
expect(result, SystemShell.bash);
6055

61-
test('for extraneous shell', () {
62-
final result = SystemShell.current(
56+
final resultWindows = SystemShell.current(
6357
environmentOverride: {
64-
'SHELL': '/usr/bin/someshell',
58+
'SHELL': r'c:\foo\bar\bash.exe',
6559
},
6660
);
6761

68-
expect(result, null);
62+
expect(resultWindows, SystemShell.bash);
63+
});
64+
65+
group('identifies no shell', () {
66+
test('for no shell env', () {
67+
final result = SystemShell.current(
68+
environmentOverride: {},
69+
);
70+
71+
expect(result, null);
72+
});
73+
74+
test('for empty shell env', () {
75+
final result = SystemShell.current(
76+
environmentOverride: {
77+
'SHELL': '',
78+
},
79+
);
80+
81+
expect(result, null);
82+
});
83+
84+
test('for extraneous shell', () {
85+
final result = SystemShell.current(
86+
environmentOverride: {
87+
'SHELL': '/usr/bin/someshell',
88+
},
89+
);
90+
91+
expect(result, null);
92+
});
6993
});
7094
});
7195
});

0 commit comments

Comments
 (0)