Skip to content

Commit

Permalink
coalaHTMLTest.py: Add tests for coala_html.py
Browse files Browse the repository at this point in the history
This commit also changes the import style for subprocess
module in `coala_html.py`. It is essential because the two
import style differs. `from module import method` loads the
method in current module. Whereas `import module` and then
`module.method()` loads the `method` from `module`. This
behaviour affects the mocking. To be able to mock the `call`
object I need to make sure the patch for `call` and the `call`
being used in `caola_html.py` are loaded from same module.
Bottomline: Mock the objects where they are being used and not
where they are being defined.
  • Loading branch information
tushar-rishav committed Aug 5, 2016
1 parent 37826d2 commit c9201b4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
8 changes: 4 additions & 4 deletions coalahtml/coala_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import json
import os
import socketserver
from subprocess import call
import subprocess
import sys
import webbrowser

Expand All @@ -35,7 +35,7 @@ def main():

dir_path = create_dir(os.path.abspath(args.dir))

if len(list(os.walk(dir_path))) > 0:
if len(list(os.walk(dir_path))) > 0: # pragma: no cover
copy_files(get_file(Constants.COALA_HTML_BASE), dir_path)

if not args.noupdate:
Expand Down Expand Up @@ -75,8 +75,8 @@ def main():
# Launch server with reference point dir_path
os.chdir(dir_path)
if not os.path.exists('bower_components'):
res = call(['bower', 'install'])
if res != 0:
res = subprocess.call(['bower', 'install'])
if res != 0: # pragma: no cover
print("Bower is required. Install from `http://bower.io/`")
sys.exit(1)

Expand Down
24 changes: 21 additions & 3 deletions tests/coalaHTMLTest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
import re
import shutil
import socketserver
import sys
import unittest
from unittest.mock import patch

from coalahtml import Constants
from coalahtml.helper import get_file
Expand All @@ -16,13 +19,23 @@
class coalaHTMLTest(unittest.TestCase):

def setUp(self):
os.chdir(os.path.dirname(os.path.dirname(__file__)))
self.old_argv = sys.argv
self.result_file = get_file(Constants.CONFIGS['results_file'], ROOT)

def tearDown(self):
sys.argv = self.old_argv

def test_output_file(self):
@patch.object(socketserver.TCPServer, 'serve_forever', autospec=True)
@patch.object(socketserver.TCPServer, 'server_close', autospec=True)
@patch('subprocess.call')
@patch('webbrowser.open')
def test_output_file(self, mock_browser,
mock_subprocess_call,
mock_server_close,
mock_serve_forever):
mock_serve_forever.side_effect = KeyboardInterrupt
mock_subprocess_call.return_value = 0 # To mock the `bower install`
update_file = ""
noupdate_file = ""
with prepare_file(["#todo this is todo"], None) as (lines, filename):
Expand All @@ -32,6 +45,7 @@ def test_output_file(self):
"-b", "LineCountBear",
"-f", re.escape(filename),
"--nolaunch")

with open(self.result_file, 'r') as fp:
update_file = fp.read()

Expand All @@ -40,10 +54,14 @@ def test_output_file(self):
"-c", os.devnull,
"-b", "LineCountBear",
"-f", re.escape(filename),
"--noupdate",
"--nolaunch")
"--noupdate")

with open(self.result_file, 'r') as fp:
noupdate_file = fp.read()

self.assertEqual(update_file, noupdate_file)
# We `chdir` before spawning the server
shutil.rmtree('../coalahtmlapp', ignore_errors=True)
self.assertTrue(mock_browser.called)
self.assertTrue(mock_serve_forever.called)
self.assertTrue(mock_server_close.called)

0 comments on commit c9201b4

Please sign in to comment.