Skip to content

Commit

Permalink
fix edit_tabs_in_editor, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
balta2ar committed Feb 16, 2020
1 parent ef5c9d8 commit 417d2e8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ brotab/node_modules
node_modules
tags
npm-debug.log
tmp
12 changes: 8 additions & 4 deletions brotab/inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from tempfile import NamedTemporaryFile
from subprocess import check_call, CalledProcessError

from brotab.platform import get_editor


def is_port_accepting_connections(port, host='127.0.0.1'):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand All @@ -30,15 +32,17 @@ def load_tabs_from_file(filename):
return Lines


def run_editor(executable: str, filename: str):
return check_call([executable, filename])


def edit_tabs_in_editor(tabs_before):
with NamedTemporaryFile() as file_:
file_name=file_.name
file_name = file_.name
file_.close()
save_tabs_to_file(tabs_before, file_name)
try:
#check_call([os.environ.get('EDITOR', 'notepad'), file_name]) # check_call([os.environ.get('EDITOR', 'nvim'), file_.name])
Editor = 'notepad' if (platform.system()=='Windows') else 'nvim'
check_call([os.environ.get('EDITOR', Editor), file_name])
run_editor(get_editor(), file_name)
tabs_after = load_tabs_from_file(file_name)
return tabs_after
except CalledProcessError:
Expand Down
11 changes: 11 additions & 0 deletions brotab/platform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os
import platform


def get_editor() -> str:
mapping = {
'Windows': 'notepad'
}
system = platform.system()
editor = mapping.get(system, 'nvim')
return os.environ.get('EDITOR', editor)
31 changes: 31 additions & 0 deletions brotab/tests/test_inout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from unittest import TestCase
from unittest.mock import patch

from brotab.inout import edit_tabs_in_editor


class TestEditor(TestCase):
@patch('brotab.inout.run_editor')
@patch('platform.system', side_effect=['Linux'])
@patch('os.environ', new={})
def test_run_editor_linux(self, _system_mock, _run_editor_mock):
assert ['1'] == edit_tabs_in_editor(['1'])
editor, _filename = _run_editor_mock.call_args[0]
assert editor == 'nvim'

@patch('brotab.inout.run_editor')
@patch('platform.system', side_effect=['Windows'])
@patch('os.environ', new={})
def test_run_editor_windows(self, _system_mock, _run_editor_mock):
assert ['1'] == edit_tabs_in_editor(['1'])
editor, _filename = _run_editor_mock.call_args[0]
assert editor == 'notepad'

@patch('brotab.inout.run_editor')
@patch('platform.system', side_effect=['Windows'])
@patch('os.environ', new={'EDITOR': 'custom'})
def test_run_editor_windows_custom(self, _system_mock, _run_editor_mock):
assert ['1'] == edit_tabs_in_editor(['1'])
editor, _filename = _run_editor_mock.call_args[0]
assert editor == 'custom'

2 changes: 1 addition & 1 deletion brotab/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Chromium(Browser):
PROFILE = 'chromium'


# @pytest.mark.skip
@pytest.mark.skip
class TestChromium(TestCase):
def setUp(self):
self._echo_server = EchoServer()
Expand Down

0 comments on commit 417d2e8

Please sign in to comment.