From cc522f8b62990da611e0b97e8eb2a7762a5e9a08 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Tue, 11 Aug 2015 18:59:29 -0700 Subject: [PATCH] improve tests by capturing stderr --- dev-requirements.txt | 1 + tests/__init__.py | 0 tests/samples/bad_config.cfg | 2 + tests/samples/output/test_help_contents | 47 +++++++++++++++++ tests/samples/output/test_missing_config_file | 8 +++ .../samples/output/test_parse_bad_config_file | 9 ++++ tests/samples/sample.cfg | 7 +++ tests/test_wakatime_base.py | 50 +++++++++++++------ tests/utils.py | 16 ++++++ 9 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/samples/bad_config.cfg create mode 100644 tests/samples/output/test_help_contents create mode 100644 tests/samples/output/test_missing_config_file create mode 100644 tests/samples/output/test_parse_bad_config_file create mode 100644 tests/samples/sample.cfg create mode 100644 tests/utils.py diff --git a/dev-requirements.txt b/dev-requirements.txt index 237b7f0..f241156 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,4 +1,5 @@ coverage==3.7.1 mock==1.3.0 nose==1.3.7 +nose-capturestderr==1.0 nose-exclude==0.3.0 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/bad_config.cfg b/tests/samples/bad_config.cfg new file mode 100644 index 0000000..5a4828e --- /dev/null +++ b/tests/samples/bad_config.cfg @@ -0,0 +1,2 @@ +[settings] +api_key diff --git a/tests/samples/output/test_help_contents b/tests/samples/output/test_help_contents new file mode 100644 index 0000000..aaf4591 --- /dev/null +++ b/tests/samples/output/test_help_contents @@ -0,0 +1,47 @@ +usage: nosetests [-h] --file file [--key KEY] [--write] [--plugin PLUGIN] + [--time time] [--lineno LINENO] [--cursorpos CURSORPOS] + [--notfile] [--proxy PROXY] [--project PROJECT] + [--alternate-project ALTERNATE_PROJECT] [--hostname HOSTNAME] + [--disableoffline] [--hidefilenames] [--exclude EXCLUDE] + [--include INCLUDE] [--logfile LOGFILE] [--apiurl API_URL] + [--config CONFIG] [--verbose] [--version] + +Common interface for the WakaTime api. + +optional arguments: + -h, --help show this help message and exit + --file file absolute path to file for current heartbeat + --key KEY your wakatime api key; uses api_key from + ~/.wakatime.conf by default + --write when set, tells api this heartbeat was triggered from + writing to a file + --plugin PLUGIN optional text editor plugin name and version for User- + Agent header + --time time optional floating-point unix epoch timestamp; uses + current time by default + --lineno LINENO optional line number; current line being edited + --cursorpos CURSORPOS + optional cursor position in the current file + --notfile when set, will accept any value for the file. for + example, a domain name or other item you want to log + time towards. + --proxy PROXY optional https proxy url; for example: + https://user:pass@localhost:8080 + --project PROJECT optional project name + --alternate-project ALTERNATE_PROJECT + optional alternate project name; auto-discovered + project takes priority + --hostname HOSTNAME hostname of current machine. + --disableoffline disables offline time logging instead of queuing + logged time + --hidefilenames obfuscate file names; will not send file names to api + --exclude EXCLUDE filename patterns to exclude from logging; POSIX regex + syntax; can be used more than once + --include INCLUDE filename patterns to log; when used in combination + with --exclude, files matching include will still be + logged; POSIX regex syntax; can be used more than once + --logfile LOGFILE defaults to ~/.wakatime.log + --apiurl API_URL heartbeats api url; for debugging with a local server + --config CONFIG defaults to ~/.wakatime.conf + --verbose turns on debug messages in log file + --version show program's version number and exit diff --git a/tests/samples/output/test_missing_config_file b/tests/samples/output/test_missing_config_file new file mode 100644 index 0000000..421f290 --- /dev/null +++ b/tests/samples/output/test_missing_config_file @@ -0,0 +1,8 @@ +usage: nosetests [-h] --file file [--key KEY] [--write] [--plugin PLUGIN] + [--time time] [--lineno LINENO] [--cursorpos CURSORPOS] + [--notfile] [--proxy PROXY] [--project PROJECT] + [--alternate-project ALTERNATE_PROJECT] [--hostname HOSTNAME] + [--disableoffline] [--hidefilenames] [--exclude EXCLUDE] + [--include INCLUDE] [--logfile LOGFILE] [--apiurl API_URL] + [--config CONFIG] [--verbose] [--version] +nosetests: error: Missing api key diff --git a/tests/samples/output/test_parse_bad_config_file b/tests/samples/output/test_parse_bad_config_file new file mode 100644 index 0000000..9150558 --- /dev/null +++ b/tests/samples/output/test_parse_bad_config_file @@ -0,0 +1,9 @@ +Traceback (most recent call last): + File "/Users/alanhamlett/git/wakatime-cli/wakatime/base.py", line 73, in parseConfigFile + configs.readfp(fh) + File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ConfigParser.py", line 324, in readfp + self._read(fp, filename) + File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ConfigParser.py", line 546, in _read + raise e +ParsingError: File contains parsing errors: tests/samples/bad_config.cfg +[line 2]: u'api_key\n' diff --git a/tests/samples/sample.cfg b/tests/samples/sample.cfg new file mode 100644 index 0000000..06acc90 --- /dev/null +++ b/tests/samples/sample.cfg @@ -0,0 +1,7 @@ +[settings] +debug = true +api_key = 1234 +api_url = https://api.wakatime.com/api/v1/heartbeats +ignore = + COMMIT_EDITMSG$ + TAG_EDITMSG$ diff --git a/tests/test_wakatime_base.py b/tests/test_wakatime_base.py index 29588d1..ea09ee8 100644 --- a/tests/test_wakatime_base.py +++ b/tests/test_wakatime_base.py @@ -1,35 +1,29 @@ # -*- coding: utf-8 -*- -import logging +import sys +from wakatime.compat import u from wakatime.packages.requests.models import Response +from . import utils try: from mock import patch except ImportError: from unittest.mock import patch -try: - # Python 2.6 - import unittest2 as unittest -except ImportError: - # Python >= 2.7 - import unittest from wakatime.base import main @patch('wakatime.packages.requests.adapters.HTTPAdapter.send') -class BaseTestCase(unittest.TestCase): - - def setUp(self): - # disable logging while testing - logging.disable(logging.CRITICAL) +class BaseTestCase(utils.TestCase): def test_help_contents(self, mock_requests): args = ['', '--help'] with self.assertRaises(SystemExit): - retval = main(args) - self.assertEquals(retval, 0) + main(args) + expected_stdout = open('tests/samples/output/test_help_contents').read() + self.assertEquals(sys.stdout.getvalue(), expected_stdout) + self.assertEquals(sys.stderr.getvalue(), '') def test_argument_parsing(self, mock_requests): response = Response() @@ -38,3 +32,31 @@ class BaseTestCase(unittest.TestCase): args = ['', '--file', 'tests/samples/emptyfile.txt', '--key', '123'] retval = main(args) self.assertEquals(retval, 0) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') + + def test_missing_config_file(self, mock_requests): + args = ['', '--file', 'tests/samples/emptyfile.txt', '--config', 'foo'] + with self.assertRaises(SystemExit): + main(args) + expected_stdout = u("Error: Could not read from config file foo\n") + expected_stderr = open('tests/samples/output/test_missing_config_file').read() + self.assertEquals(sys.stdout.getvalue(), expected_stdout) + self.assertEquals(sys.stderr.getvalue(), expected_stderr) + + def test_parse_config_file(self, mock_requests): + response = Response() + response.status_code = 201 + mock_requests.return_value = response + args = ['', '--file', 'tests/samples/emptyfile.txt', '--config', 'tests/samples/sample.cfg'] + retval = main(args) + self.assertEquals(retval, 0) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') + + def test_parse_bad_config_file(self, mock_requests): + args = ['', '--file', 'tests/samples/emptyfile.txt', '--config', 'tests/samples/bad_config.cfg'] + retval = main(args) + self.assertEquals(retval, 103) + self.assertTrue('ParsingError' in sys.stdout.getvalue()) + self.assertEquals(sys.stderr.getvalue(), '') diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..cd73e21 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- + +import logging + +try: + # Python 2.6 + import unittest2 as unittest +except ImportError: + # Python >= 2.7 + import unittest + +class TestCase(unittest.TestCase): + + def setUp(self): + # disable logging while testing + logging.disable(logging.CRITICAL)