Skip to content

Commit

Permalink
fix: add system shell identification by heuristics (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
renancaraujo authored Dec 8, 2022
1 parent 000486e commit d30f59e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 38 deletions.
14 changes: 12 additions & 2 deletions lib/src/system_shell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ enum SystemShell {
}) {
final environment = environmentOverride ?? Platform.environment;

// TODO(renancaraujo): this detects the "login shell", which can be
// different from the actual shell.
// Heuristic: if ZSH_NAME is set, must be zsh
final isZSH = environment['ZSH_NAME'] != null;
if (isZSH) {
return SystemShell.zsh;
}

// Heuristic: if BASH is set, must be bash
final isBash = environment['BASH'] != null;
if (isBash) {
return SystemShell.bash;
}

final envShell = environment['SHELL'];
if (envShell == null || envShell.isEmpty) return null;

Expand Down
96 changes: 60 additions & 36 deletions test/src/system_shell_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,93 @@ import 'package:test/test.dart';

void main() {
group('SystemShell', () {
group('fromCurrentShell', () {
group('current', () {
test('instantiated without env', () {
expect(
SystemShell.current,
returnsNormally,
);
});

test('identifies zsh', () {
final result = SystemShell.current(
environmentOverride: {
'SHELL': '/foo/bar/zsh',
},
);

expect(result, SystemShell.zsh);
});

test('identifies bash shell', () {
final result = SystemShell.current(
environmentOverride: {
'SHELL': '/foo/bar/bash',
},
);
group('Heuristics', () {
test('identifies zsh', () {
final result = SystemShell.current(
environmentOverride: {
'ZSH_NAME': 'zsh',
},
);

expect(result, SystemShell.bash);
expect(result, SystemShell.zsh);
});

final resultWindows = SystemShell.current(
environmentOverride: {
'SHELL': r'c:\foo\bar\bash.exe',
},
);
test('identifies bash', () {
final result = SystemShell.current(
environmentOverride: {
'BASH': '/bin/bash',
},
);

expect(resultWindows, SystemShell.bash);
expect(result, SystemShell.bash);
});
});

group('identifies no shell', () {
test('for no shell env', () {
group(r'When checking $SHELL', () {
test('identifies zsh', () {
final result = SystemShell.current(
environmentOverride: {},
environmentOverride: {
'SHELL': '/foo/bar/zsh',
},
);

expect(result, null);
expect(result, SystemShell.zsh);
});

test('for empty shell env', () {
test('identifies bash shell', () {
final result = SystemShell.current(
environmentOverride: {
'SHELL': '',
'SHELL': '/foo/bar/bash',
},
);

expect(result, null);
});
expect(result, SystemShell.bash);

test('for extraneous shell', () {
final result = SystemShell.current(
final resultWindows = SystemShell.current(
environmentOverride: {
'SHELL': '/usr/bin/someshell',
'SHELL': r'c:\foo\bar\bash.exe',
},
);

expect(result, null);
expect(resultWindows, SystemShell.bash);
});

group('identifies no shell', () {
test('for no shell env', () {
final result = SystemShell.current(
environmentOverride: {},
);

expect(result, null);
});

test('for empty shell env', () {
final result = SystemShell.current(
environmentOverride: {
'SHELL': '',
},
);

expect(result, null);
});

test('for extraneous shell', () {
final result = SystemShell.current(
environmentOverride: {
'SHELL': '/usr/bin/someshell',
},
);

expect(result, null);
});
});
});
});
Expand Down

0 comments on commit d30f59e

Please sign in to comment.