diff --git a/lib/src/system_shell.dart b/lib/src/system_shell.dart index eef1c0a..77963cd 100644 --- a/lib/src/system_shell.dart +++ b/lib/src/system_shell.dart @@ -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; diff --git a/test/src/system_shell_test.dart b/test/src/system_shell_test.dart index d25e73f..163bbfd 100644 --- a/test/src/system_shell_test.dart +++ b/test/src/system_shell_test.dart @@ -3,7 +3,7 @@ import 'package:test/test.dart'; void main() { group('SystemShell', () { - group('fromCurrentShell', () { + group('current', () { test('instantiated without env', () { expect( SystemShell.current, @@ -11,61 +11,85 @@ void main() { ); }); - 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); + }); }); }); });