From 3167bbe264739803929776a9a4fe24295bf54394 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 19 May 2017 15:10:25 +0200 Subject: [PATCH 01/10] FEATURE: Add first tests --- rplugin/python3/__init__.py | 0 rplugin/python3/neotags.py | 1 + rplugin/test/__init__.py | 0 rplugin/test/test_neotags.py | 61 ++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 rplugin/python3/__init__.py create mode 100644 rplugin/test/__init__.py create mode 100644 rplugin/test/test_neotags.py diff --git a/rplugin/python3/__init__.py b/rplugin/python3/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/rplugin/python3/neotags.py b/rplugin/python3/neotags.py index b2b7f5e..73b6dcf 100644 --- a/rplugin/python3/neotags.py +++ b/rplugin/python3/neotags.py @@ -12,6 +12,7 @@ class NeotagsPlugin(object): def __init__(self, nvim): self.nvim = nvim # Perhaps debugging settings? + # Like autotags, configure regex compared to filename to ignore, e.g. fugitive buffers, git commits, etc. self.options = { 'tags_filename': 'tags', 'ctags_cmd': 'ctags', diff --git a/rplugin/test/__init__.py b/rplugin/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/rplugin/test/test_neotags.py b/rplugin/test/test_neotags.py new file mode 100644 index 0000000..9c3c78d --- /dev/null +++ b/rplugin/test/test_neotags.py @@ -0,0 +1,61 @@ +import unittest +from unittest.mock import Mock +from unittest.mock import MagicMock +import neovim + +from python3.neotags import NeotagsPlugin + + +class TestNeotagsPlugin(unittest.TestCase): + + def setUp(self): + self.plugin = NeotagsPlugin(Mock(neovim.api.nvim)) + + def test_default_options(self): + self.assertEqual('ctags', self.plugin.options['ctags_cmd']) + self.assertEqual('tags', self.plugin.options['tags_filename']) + self.assertEqual(False, self.plugin.options['logging']) + + def test_default_options(self): + self.plugin.nvim.configure_mock(vars={}) + self.plugin.nvim.vars['neotags_tags_filename'] = 'new_tags' + self.plugin.nvim.vars['neotags_ctags_cmd'] = 'new_ctags' + self.plugin.nvim.vars['neotags_logging'] = True + + self.plugin.update_settings() + + self.assertEqual('new_tags', self.plugin.options['tags_filename']) + self.assertEqual('new_ctags', self.plugin.options['ctags_cmd']) + self.assertEqual(True, self.plugin.options['logging']) + + def test_debug_logs_to_nvim_when_logging_is_active(self): + self.plugin.options['logging'] = True + self.plugin.nvim.out_write = MagicMock() + + self.plugin.debug('some message') + self.plugin.nvim.out_write.assert_called_once_with( + "neotags > some message\n" + ) + + def test_debug_does_not_log_to_nvim_when_logging_is_inactive(self): + self.plugin.options['logging'] = False + self.plugin.nvim.out_write = MagicMock() + + self.plugin.debug('some message') + self.plugin.nvim.out_write.assert_not_called() + + def test_error_logs_to_nvim_when_logging_is_active(self): + self.plugin.options['logging'] = True + self.plugin.nvim.err_write = MagicMock() + + self.plugin.error('some message') + self.plugin.nvim.err_write.assert_called_once_with( + "neotags > some message\n" + ) + + def test_error_does_not_log_to_nvim_when_logging_is_inactive(self): + self.plugin.options['logging'] = False + self.plugin.nvim.err_write = MagicMock() + + self.plugin.error('some message') + self.plugin.nvim.err_write.assert_not_called() From 44004f916baae8994c52d460113c47e442fc2c7a Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 19 May 2017 15:13:22 +0200 Subject: [PATCH 02/10] FEATURE: Add test execution to gitlab --- .gitlab-ci.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..5442cbf --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,14 @@ +lint:coding-guideline: + image: python:3-alpine + before_script: + - pip install pep8 + script: + - pep8 . + +test: + image: python:3-alpine + before_script: + - pip install neovim + - cd rplugin + script: + - python -m unittest test.test_neotags From d37ee394cba101c2fc42b1d23a80080db86f5a93 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 19 May 2017 15:39:22 +0200 Subject: [PATCH 03/10] TASK: Fix pep8 line length --- rplugin/python3/neotags.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rplugin/python3/neotags.py b/rplugin/python3/neotags.py index 73b6dcf..44ec7a6 100644 --- a/rplugin/python3/neotags.py +++ b/rplugin/python3/neotags.py @@ -12,7 +12,8 @@ class NeotagsPlugin(object): def __init__(self, nvim): self.nvim = nvim # Perhaps debugging settings? - # Like autotags, configure regex compared to filename to ignore, e.g. fugitive buffers, git commits, etc. + # Like autotags, configure regex compared to filename to ignore, e.g. + # fugitive buffers, git commits, etc. self.options = { 'tags_filename': 'tags', 'ctags_cmd': 'ctags', From 8291de09958bf6b8aba4cdba3b0c29844258fdc9 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 19 May 2017 15:43:40 +0200 Subject: [PATCH 04/10] TASK: Add ci dependency to build neovim module --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5442cbf..a0143a2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,6 +8,7 @@ lint:coding-guideline: test: image: python:3-alpine before_script: + - apk add --no-cache gcc - pip install neovim - cd rplugin script: From 18495e17bc8ff3fc3bf5119ac315d11a912de818 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 19 May 2017 15:48:20 +0200 Subject: [PATCH 05/10] TASK: Add own docker image --- .gitlab-ci.yml | 22 ++++++++++++++++++++-- Dockerfile | 12 ++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 Dockerfile diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a0143a2..1b712d3 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,12 +1,30 @@ +stages: + - build + - test + +build:image: + image: docker:git + stage: build + services: + - docker:dind + script: + - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com + - docker build -t registry.gitlab.com/danielsiepmann/neotags . + - docker push registry.gitlab.com/neotags/neotags:latest + only: + - master + lint:coding-guideline: - image: python:3-alpine + image: 'registry.gitlab.com/danielsiepmann/neotags:latest' + stage: test before_script: - pip install pep8 script: - pep8 . test: - image: python:3-alpine + image: 'registry.gitlab.com/danielsiepmann/neotags:latest' + stage: test before_script: - apk add --no-cache gcc - pip install neovim diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f47fa5c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM alpine:3.5 + +MAINTAINER Daniel Siepmann +LABEL Description="This image should provide environment to lint neotags" Vendor="DanielSiepmann" Version="1.0" + +RUN apk update && apk upgrade + +# Install dependencies +RUN apk add python3 py-pip + +# Clean APK cache +RUN rm -rf /var/cache/apk/* From 84d4bf35ef84b600d13ad1445c0d01da409078ad Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 19 May 2017 15:52:19 +0200 Subject: [PATCH 06/10] Install python dependencies in docker image --- .gitlab-ci.yml | 10 +++------- Dockerfile | 3 +++ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1b712d3..54ad897 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,19 +6,17 @@ build:image: image: docker:git stage: build services: - - docker:dind + - docker:dind script: - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com - docker build -t registry.gitlab.com/danielsiepmann/neotags . - docker push registry.gitlab.com/neotags/neotags:latest - only: - - master + # only: + # - master lint:coding-guideline: image: 'registry.gitlab.com/danielsiepmann/neotags:latest' stage: test - before_script: - - pip install pep8 script: - pep8 . @@ -26,8 +24,6 @@ test: image: 'registry.gitlab.com/danielsiepmann/neotags:latest' stage: test before_script: - - apk add --no-cache gcc - - pip install neovim - cd rplugin script: - python -m unittest test.test_neotags diff --git a/Dockerfile b/Dockerfile index f47fa5c..a41c982 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,3 +10,6 @@ RUN apk add python3 py-pip # Clean APK cache RUN rm -rf /var/cache/apk/* + +# Install dependencies +RUN pip install pep8 neovim From 3c29beb0d5b5f55fa13229b02c75434e27051466 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 19 May 2017 16:04:38 +0200 Subject: [PATCH 07/10] TASK: Finish docker --- Dockerfile | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index a41c982..7cf3352 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,8 @@ -FROM alpine:3.5 +FROM python:3 MAINTAINER Daniel Siepmann LABEL Description="This image should provide environment to lint neotags" Vendor="DanielSiepmann" Version="1.0" -RUN apk update && apk upgrade - # Install dependencies -RUN apk add python3 py-pip - -# Clean APK cache -RUN rm -rf /var/cache/apk/* - -# Install dependencies -RUN pip install pep8 neovim +RUN pip install --upgrade pip +RUN pip install --no-cache-dir pep8 neovim From 3c3289df3c17f5fa59278a2b6f07e14c96288672 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 19 May 2017 16:08:45 +0200 Subject: [PATCH 08/10] TASK: Fix docker push url --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 54ad897..cdbc48a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,7 +10,7 @@ build:image: script: - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com - docker build -t registry.gitlab.com/danielsiepmann/neotags . - - docker push registry.gitlab.com/neotags/neotags:latest + - docker push registry.gitlab.com/danielsiepmann/neotags:latest # only: # - master From 73ad18d317c83d2a43b7d5f2b120055ff21e4a3f Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 19 May 2017 16:51:34 +0200 Subject: [PATCH 09/10] TASK: Add further tests --- rplugin/test/templates/example.py | 7 +++ rplugin/test/templates/expected_tags | 6 +++ rplugin/test/test_neotags.py | 69 +++++++++++++++++++++++++++- 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 rplugin/test/templates/example.py create mode 100644 rplugin/test/templates/expected_tags diff --git a/rplugin/test/templates/example.py b/rplugin/test/templates/example.py new file mode 100644 index 0000000..f271883 --- /dev/null +++ b/rplugin/test/templates/example.py @@ -0,0 +1,7 @@ +class SomeClass(object): + + def __init__(self): + pass + + def method(self): + pass diff --git a/rplugin/test/templates/expected_tags b/rplugin/test/templates/expected_tags new file mode 100644 index 0000000..d086ef2 --- /dev/null +++ b/rplugin/test/templates/expected_tags @@ -0,0 +1,6 @@ +!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ +!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ +!_TAG_PROGRAM_AUTHOR Universal Ctags Team // +!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/ +!_TAG_PROGRAM_URL https://ctags.io/ /official site/ +!_TAG_PROGRAM_VERSION 0.0.0 /4f741b9/ diff --git a/rplugin/test/test_neotags.py b/rplugin/test/test_neotags.py index 9c3c78d..486aa89 100644 --- a/rplugin/test/test_neotags.py +++ b/rplugin/test/test_neotags.py @@ -1,14 +1,20 @@ -import unittest from unittest.mock import Mock from unittest.mock import MagicMock +from pyfakefs import fake_filesystem_unittest import neovim +import os from python3.neotags import NeotagsPlugin -class TestNeotagsPlugin(unittest.TestCase): +class TestNeotagsPlugin(fake_filesystem_unittest.TestCase): def setUp(self): + self.templates_dirname = os.path.join( + os.path.dirname(__file__), + 'templates' + ) + self.setUpPyfakefs() self.plugin = NeotagsPlugin(Mock(neovim.api.nvim)) def test_default_options(self): @@ -59,3 +65,62 @@ class TestNeotagsPlugin(unittest.TestCase): self.plugin.error('some message') self.plugin.nvim.err_write.assert_not_called() + + def test_get_tags_file_in_same_folder(self): + filename = '/var/data/some_file.py' + expected_tags_filename = '/var/data/tags' + + self.fs.CreateFile(filename) + self.fs.CreateFile(expected_tags_filename) + + self.assertEqual( + expected_tags_filename, + self.plugin.get_tags_file(filename) + ) + + def test_get_tags_file_in_parent_folder(self): + filename = '/var/data/some_file.py' + expected_tags_filename = '/var/tags' + + self.fs.CreateFile(filename) + self.fs.CreateFile(expected_tags_filename) + + self.assertEqual( + expected_tags_filename, + self.plugin.get_tags_file(filename) + ) + + def test_get_tags_file_in_root_folder(self): + filename = '/var/data/some_file.py' + expected_tags_filename = '/tags' + + self.fs.CreateFile(filename) + self.fs.CreateFile(expected_tags_filename) + + self.assertEqual( + expected_tags_filename, + self.plugin.get_tags_file(filename) + ) + + def test_strips_existing_tags(self): + filename = '/var/data/example.py' + tags_file = '/var/data/tags' + expected_tags_file = os.path.join( + self.templates_dirname, + 'expected_tags' + ) + + self.copyRealFile( + os.path.join(self.templates_dirname, 'example.py'), + os.path.join(os.path.dirname(filename), os.path.basename(filename)) + ) + self.copyRealFile( + os.path.join(self.templates_dirname, 'tags'), + os.path.join( + os.path.dirname(tags_file), os.path.basename(tags_file) + ) + ) + + self.plugin.strip_existing_tags(tags_file, os.path.basename(filename)) + with open(tags_file) as f, open(expected_tags_file) as e: + self.assertEqual(e.read(), f.read()) From 872968b582f7c043e587f2f474de1dc87b8aeee4 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 19 May 2017 16:52:06 +0200 Subject: [PATCH 10/10] TASK: Update dev dependencies and provide readme --- Dockerfile | 2 +- readme.rst | 11 +++++++++++ rplugin/test/templates/tags | 9 +++++++++ rplugin/test/test_neotags.py | 4 ++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 rplugin/test/templates/tags diff --git a/Dockerfile b/Dockerfile index 7cf3352..e1406b4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,4 +5,4 @@ LABEL Description="This image should provide environment to lint neotags" Vendor # Install dependencies RUN pip install --upgrade pip -RUN pip install --no-cache-dir pep8 neovim +RUN pip install --no-cache-dir pep8 neovim pyfakefs diff --git a/readme.rst b/readme.rst index 6068f46..995af81 100644 --- a/readme.rst +++ b/readme.rst @@ -38,3 +38,14 @@ The following options are available: - ``let g:neotags_logging = 0`` Defines whether to log anything to vims messages. + +Development +=========== + +Install dependencies: + +- ``pip install pyfakefs`` + +Run tests: + +``cd rplugin && python -m unittest test.test_neotags`` diff --git a/rplugin/test/templates/tags b/rplugin/test/templates/tags new file mode 100644 index 0000000..b2352b5 --- /dev/null +++ b/rplugin/test/templates/tags @@ -0,0 +1,9 @@ +!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ +!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ +!_TAG_PROGRAM_AUTHOR Universal Ctags Team // +!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/ +!_TAG_PROGRAM_URL https://ctags.io/ /official site/ +!_TAG_PROGRAM_VERSION 0.0.0 /4f741b9/ +SomeClass example.py /^class SomeClass(object):$/;" kind:class line:1 language:Python inherits:object access:public end:7 +__init__ example.py /^ def __init__(self):$/;" kind:member line:3 language:Python scope:class:SomeClass access:public signature:(self) end:5 +method example.py /^ def method(self):$/;" kind:member line:6 language:Python scope:class:SomeClass access:public signature:(self) end:7 diff --git a/rplugin/test/test_neotags.py b/rplugin/test/test_neotags.py index 486aa89..25e5b26 100644 --- a/rplugin/test/test_neotags.py +++ b/rplugin/test/test_neotags.py @@ -110,6 +110,10 @@ class TestNeotagsPlugin(fake_filesystem_unittest.TestCase): 'expected_tags' ) + self.copyRealFile( + expected_tags_file, + expected_tags_file + ) self.copyRealFile( os.path.join(self.templates_dirname, 'example.py'), os.path.join(os.path.dirname(filename), os.path.basename(filename))