diff --git a/readme.md b/readme.md index 586074b..70f1e40 100644 --- a/readme.md +++ b/readme.md @@ -59,3 +59,20 @@ If you use [fish shell](https://fishshell.com/), specify this in your settings: ``` This will instruct Sublime PHPUnit to connect the commands using fish's `; and` instead of bash's `&&`. + +## Running tests over ssh (for VMs like Laravel Homestead) + +```json +{ + "phpunit-sublime-ssh": true, + "phpunit-sublime-ssh-host": "host", + "phpunit-sublime-ssh-user": "user", + "phpunit-sublime-ssh-port": 22, + "phpunit-sublime-ssh-paths": { + "/your/local/path": "/your/remote/path" + }, + "phpunit-sublime-ssh-command-suffix": "--configuration /your/specific/path/to/phpunit.xml", + "phpunit-sublime-ssh-command-prefix": "TERM=xterm-256color" +} +``` +Default ssh port is 22, so setting phpunit-sublime-ssh-port is optional. Also phpunit-sublime-ssh-command-suffix and phpunit-sublime-ssh-command-prefix are of course optional. diff --git a/sublime-phpunit.py b/sublime-phpunit.py index 259d76f..30f1902 100644 --- a/sublime-phpunit.py +++ b/sublime-phpunit.py @@ -9,6 +9,7 @@ class PhpunitTestCommand(sublime_plugin.WindowCommand): lastTestCommand = False + lastFilter = False def get_setting(self, key, default=None): return sublime.load_settings("Preferences.sublime-settings").get(key, default) @@ -73,6 +74,11 @@ def find_phpunit_bin(self, directory): def run_in_terminal(self, command): osascript_command = 'osascript ' + useSsh = self.get_setting('phpunit-sublime-ssh', False) + + if useSsh: + command = self.localToSshCommand(self.filter); + if self.get_setting('phpunit-sublime-terminal', 'Term') == 'iTerm': osascript_command += '"' + os.path.dirname(os.path.realpath(__file__)) + '/open_iterm.applescript"' osascript_command += ' "' + command + '"' @@ -81,14 +87,40 @@ def run_in_terminal(self, command): osascript_command += ' "' + command + '"' osascript_command += ' "PHPUnit Tests"' + self.lastFilter = self.filter self.lastTestCommand = command os.system(osascript_command) + def localToSshCommand(self, phpunitFilter): + localToHostProjectRoots = self.get_setting('phpunit-sublime-ssh-paths', {}) + filePath = self.window.active_view().file_name() + + for localRoot in localToHostProjectRoots: + if localRoot in filePath: + localProjectRoot = localRoot + hostProjectRoot = localToHostProjectRoots[localRoot] + + sshCommand = "ssh -t -p" + str(self.get_setting('phpunit-sublime-ssh-port', 22)) + " " + self.get_setting('phpunit-sublime-ssh-user') + "@" + self.get_setting('phpunit-sublime-ssh-host') + " 'cd " + hostProjectRoot + "; vendor/bin/phpunit' " + phpunitFilter.replace(localProjectRoot, hostProjectRoot); + + prefixCommand = self.get_setting('phpunit-sublime-ssh-command-prefix', False) + + if prefixCommand: + sshCommand = prefixCommand + ' ' + sshCommand + + suffixCommand = self.get_setting('phpunit-sublime-ssh-command-suffix', False) + + if suffixCommand: + sshCommand = sshCommand + ' ' + suffixCommand + + return sshCommand + class RunPhpunitTestCommand(PhpunitTestCommand): def run(self, *args, **kwargs): file_name, phpunit_config_path, phpunit_bin, active_view, directory = self.get_paths() + self.filter = file_name + self.run_in_terminal('cd ' + phpunit_config_path + self.get_cmd_connector() + phpunit_bin + ' ' + file_name) class RunAllPhpunitTestsCommand(PhpunitTestCommand): @@ -96,6 +128,8 @@ class RunAllPhpunitTestsCommand(PhpunitTestCommand): def run(self, *args, **kwargs): file_name, phpunit_config_path, phpunit_bin, active_view, directory = self.get_paths() + self.filter = '' + self.run_in_terminal('cd ' + phpunit_config_path + self.get_cmd_connector() + phpunit_bin) @@ -106,6 +140,8 @@ def run(self, *args, **kwargs): current_function = self.get_current_function(active_view) + self.filter = file_name + " --filter '/::" + current_function + "$/'" + self.run_in_terminal('cd ' + phpunit_config_path + self.get_cmd_connector() + phpunit_bin + ' ' + file_name + " --filter '/::" + current_function + "$/'") class RunLastPhpunitTestCommand(PhpunitTestCommand): @@ -116,6 +152,7 @@ def run(self, *args, **kwargs): if 'Test' in file_name: RunSinglePhpunitTestCommand.run(self, args, kwargs); elif self.lastTestCommand: + self.filter = self.lastFilter self.run_in_terminal(self.lastTestCommand) class RunPhpunitTestsInDirCommand(PhpunitTestCommand): @@ -123,6 +160,8 @@ class RunPhpunitTestsInDirCommand(PhpunitTestCommand): def run(self, *args, **kwargs): file_name, phpunit_config_path, phpunit_bin, active_view, directory = self.get_paths() + self.filter = directory + self.run_in_terminal('cd ' + phpunit_config_path + self.get_cmd_connector() + phpunit_bin + ' ' + directory) class RunSingleDuskTestCommand(PhpunitTestCommand):