Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add system shell identification by heuristics #31

Merged
merged 3 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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