diff --git a/tests/samples/emptyfile.txt b/tests/samples/codefiles/emptyfile.txt similarity index 100% rename from tests/samples/emptyfile.txt rename to tests/samples/codefiles/emptyfile.txt diff --git a/tests/samples/codefile.py b/tests/samples/codefiles/python.py similarity index 100% rename from tests/samples/codefile.py rename to tests/samples/codefiles/python.py diff --git a/tests/samples/codefiles/see.c b/tests/samples/codefiles/see.c new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/see.h b/tests/samples/codefiles/see.h new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/seeplusplus.cpp b/tests/samples/codefiles/seeplusplus.cpp new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/codefiles/seeplusplus.h b/tests/samples/codefiles/seeplusplus.h new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/twolinefile.txt b/tests/samples/codefiles/twolinefile.txt similarity index 100% rename from tests/samples/twolinefile.txt rename to tests/samples/codefiles/twolinefile.txt diff --git a/tests/test_wakatime_languages.py b/tests/test_wakatime_languages.py index fec6d24..97cf4af 100644 --- a/tests/test_wakatime_languages.py +++ b/tests/test_wakatime_languages.py @@ -37,7 +37,7 @@ class LanguagesTestCase(utils.TestCase): self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response now = u(int(time.time())) - entity = 'tests/samples/codefile.py' + entity = 'tests/samples/codefiles/python.py' config = 'tests/samples/sample.cfg' args = ['--file', entity, '--config', config, '--time', now] @@ -54,8 +54,8 @@ class LanguagesTestCase(utils.TestCase): heartbeat = { 'language': u('Python'), 'lines': 26, - 'entity': os.path.abspath(entity), - 'project': u(os.path.basename(os.path.abspath('.'))), + 'entity': os.path.realpath(entity), + 'project': u(os.path.basename(os.path.realpath('.'))), 'dependencies': ANY, 'branch': os.environ.get('TRAVIS_COMMIT', ANY), 'time': float(now), @@ -76,3 +76,30 @@ class LanguagesTestCase(utils.TestCase): self.assertIn(dep, self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies']) self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + + def test_language_detected_for_header_file(self): + response = Response() + response.status_code = 500 + self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response + + now = u(int(time.time())) + config = 'tests/samples/sample.cfg' + + entity = 'tests/samples/codefiles/see.h' + + args = ['--file', entity, '--config', config, '--time', now] + + retval = execute(args) + self.assertEquals(retval, 102) + + language = u('C') + self.assertEqual(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['language'], language) + + entity = 'tests/samples/codefiles/seeplusplus.h' + args[1] = entity + + retval = execute(args) + self.assertEquals(retval, 102) + + language = u('C++') + self.assertEqual(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['language'], language) diff --git a/tests/test_wakatime_main.py b/tests/test_wakatime_main.py index bcb19fe..49128a5 100644 --- a/tests/test_wakatime_main.py +++ b/tests/test_wakatime_main.py @@ -47,7 +47,7 @@ class BaseTestCase(utils.TestCase): response.status_code = 201 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - args = ['--file', 'tests/samples/twolinefile.txt', '--key', '123', '--config', 'tests/samples/sample.cfg'] + args = ['--file', 'tests/samples/codefiles/twolinefile.txt', '--key', '123', '--config', 'tests/samples/sample.cfg'] retval = execute(args) self.assertEquals(retval, 0) @@ -62,7 +62,7 @@ class BaseTestCase(utils.TestCase): self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() def test_missing_config_file(self): - args = ['--file', 'tests/samples/emptyfile.txt', '--config', 'foo'] + args = ['--file', 'tests/samples/codefiles/emptyfile.txt', '--config', 'foo'] with self.assertRaises(SystemExit): execute(args) expected_stdout = u("Error: Could not read from config file foo\n") @@ -77,7 +77,7 @@ class BaseTestCase(utils.TestCase): response.status_code = 201 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - args = ['--file', 'tests/samples/emptyfile.txt', '--config', 'tests/samples/sample.cfg'] + args = ['--file', 'tests/samples/codefiles/emptyfile.txt', '--config', 'tests/samples/sample.cfg'] retval = execute(args) self.assertEquals(retval, 0) self.assertEquals(sys.stdout.getvalue(), '') @@ -91,7 +91,7 @@ class BaseTestCase(utils.TestCase): self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() def test_bad_config_file(self): - args = ['--file', 'tests/samples/emptyfile.txt', '--config', 'tests/samples/bad_config.cfg'] + args = ['--file', 'tests/samples/codefiles/emptyfile.txt', '--config', 'tests/samples/bad_config.cfg'] retval = execute(args) self.assertEquals(retval, 103) self.assertIn('ParsingError', sys.stdout.getvalue()) @@ -107,7 +107,7 @@ class BaseTestCase(utils.TestCase): self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response now = u(int(time.time())) - entity = 'tests/samples/twolinefile.txt' + entity = 'tests/samples/codefiles/twolinefile.txt' config = 'tests/samples/sample.cfg' args = ['--file', entity, '--key', '123', '--config', config, '--time', now] @@ -148,7 +148,7 @@ class BaseTestCase(utils.TestCase): self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response now = u(int(time.time())) - entity = 'tests/samples/twolinefile.txt' + entity = 'tests/samples/codefiles/twolinefile.txt' config = 'tests/samples/paranoid.cfg' args = ['--file', entity, '--key', '123', '--config', config, '--time', now] @@ -190,7 +190,7 @@ class BaseTestCase(utils.TestCase): now = u(int(time.time())) - args = ['--file', 'tests/samples/twolinefile.txt', '--key', '123', + args = ['--file', 'tests/samples/codefiles/twolinefile.txt', '--key', '123', '--config', 'tests/samples/paranoid.cfg', '--time', now] @@ -231,7 +231,7 @@ class BaseTestCase(utils.TestCase): now = u(int(time.time())) - args = ['--file', 'tests/samples/twolinefile.txt', '--key', '123', + args = ['--file', 'tests/samples/codefiles/twolinefile.txt', '--key', '123', '--config', 'tests/samples/paranoid.cfg', '--time', now] @@ -253,7 +253,7 @@ class BaseTestCase(utils.TestCase): self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response now = u(int(time.time())) - entity = 'tests/samples/twolinefile.txt' + entity = 'tests/samples/codefiles/twolinefile.txt' config = 'tests/samples/sample.cfg' args = ['--file', entity, '--alternate-project', 'xyz', '--config', config, '--time', now] @@ -294,7 +294,7 @@ class BaseTestCase(utils.TestCase): self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response now = u(int(time.time())) - entity = 'tests/samples/twolinefile.txt' + entity = 'tests/samples/codefiles/twolinefile.txt' config = 'tests/samples/sample.cfg' args = ['--file', entity, '--project', 'xyz', '--config', config, '--time', now] diff --git a/tests/test_wakatime_offlinequeue.py b/tests/test_wakatime_offlinequeue.py index f667a0a..9bb3bac 100644 --- a/tests/test_wakatime_offlinequeue.py +++ b/tests/test_wakatime_offlinequeue.py @@ -27,7 +27,7 @@ class OfflineQueueTestCase(utils.TestCase): self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response now = u(int(time.time())) - entity = 'tests/samples/twolinefile.txt' + entity = 'tests/samples/codefiles/twolinefile.txt' config = 'tests/samples/sample.cfg' args = ['--file', entity, '--config', config, '--time', now] @@ -43,7 +43,7 @@ class OfflineQueueTestCase(utils.TestCase): self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response now = u(int(time.time())) - entity = 'tests/samples/twolinefile.txt' + entity = 'tests/samples/codefiles/twolinefile.txt' config = 'tests/samples/sample.cfg' args = ['--file', entity, '--config', config, '--time', now] @@ -59,7 +59,7 @@ class OfflineQueueTestCase(utils.TestCase): self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response now = u(int(time.time())) - entity = 'tests/samples/twolinefile.txt' + entity = 'tests/samples/codefiles/twolinefile.txt' config = 'tests/samples/sample.cfg' args = ['--file', entity, '--config', config, '--time', now] diff --git a/wakatime/stats.py b/wakatime/stats.py index e02a0eb..ce60933 100644 --- a/wakatime/stats.py +++ b/wakatime/stats.py @@ -123,7 +123,12 @@ def get_language_from_extension(file_name): """Returns a matching language for the given file extension. """ - extension = os.path.splitext(file_name)[1].lower() + filepart, extension = os.path.splitext(file_name) + + if os.path.exists(u('{0}{1}').format(u(filepart), u('.c'))) or os.path.exists(u('{0}{1}').format(u(filepart), u('.C'))): + return 'C' + + extension = extension.lower() if extension == '.h': directory = os.path.dirname(file_name) available_files = os.listdir(directory)