Add unit tests for version script 65/4065/1
authorThanh Ha <thanh.ha@linuxfoundation.org>
Sun, 5 Mar 2017 04:20:39 +0000 (23:20 -0500)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Sun, 5 Mar 2017 04:39:52 +0000 (23:39 -0500)
Adds unit tests for the version script to ensure it works in the use
cases OpenDaylight requires the script for. Unit Tests run against
Python 3 as we are only supporting Python 3 for lftools at the moment.

- Fix version script not running 'release' command
- Add pytest
- Test version script for 3 use cases:
    1) Version bump for release
    2) Version bump by x.y.(z+1) post release
    3) Version bump by x.(y+1).z

Change-Id: Ib36d9a84e2769863083400c434e1539b302869a8
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
.gitignore
setup.py
shell/version
tests/fixtures/pom.xml [new file with mode: 0644]
tests/fixtures/pom.xml.expected-bump [new file with mode: 0644]
tests/fixtures/pom.xml.expected-release [new file with mode: 0644]
tests/fixtures/pom.xml.expected-release-bump [new file with mode: 0644]
tests/test_version.py [new file with mode: 0644]
tox.ini

index d366ccc..5666024 100644 (file)
@@ -15,6 +15,8 @@ target/
 .project
 
 # Python
+.cache/
+.eggs/
 .tox/
 __pycache__/
 *.egg-info/
index 4f225a0..04bc90f 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -32,6 +32,8 @@ setup(
         'tests.*',
         'tests'
     ]),
+    setup_requires=['pytest-runner'],
+    tests_require=['pytest'],
     entry_points='''
         [console_scripts]
         lftools=lftools.cli:cli
index f17a73c..12a7499 100755 (executable)
@@ -59,6 +59,7 @@ version() {
         release )
             RELEASE_TAG=$1
             echo "Bumping SNAPSHOTS to $RELEASE_TAG"
+            version_release
             exit 0
             ;;
         * )
diff --git a/tests/fixtures/pom.xml b/tests/fixtures/pom.xml
new file mode 100644 (file)
index 0000000..0b4d73d
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.linuxfoundation.releng</groupId>
+  <artifactId>test-artifact</artifactId>
+  <version>1.0.0-SNAPSHOT</version>
+</project>
diff --git a/tests/fixtures/pom.xml.expected-bump b/tests/fixtures/pom.xml.expected-bump
new file mode 100644 (file)
index 0000000..337f387
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.linuxfoundation.releng</groupId>
+  <artifactId>test-artifact</artifactId>
+  <version>1.1.0-SNAPSHOT</version>
+</project>
diff --git a/tests/fixtures/pom.xml.expected-release b/tests/fixtures/pom.xml.expected-release
new file mode 100644 (file)
index 0000000..f8769e3
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.linuxfoundation.releng</groupId>
+  <artifactId>test-artifact</artifactId>
+  <version>1.0.0-TestRelease</version>
+</project>
diff --git a/tests/fixtures/pom.xml.expected-release-bump b/tests/fixtures/pom.xml.expected-release-bump
new file mode 100644 (file)
index 0000000..24b6ae3
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.linuxfoundation.releng</groupId>
+  <artifactId>test-artifact</artifactId>
+  <version>1.0.1-SNAPSHOT</version>
+</project>
diff --git a/tests/test_version.py b/tests/test_version.py
new file mode 100644 (file)
index 0000000..9d8fe84
--- /dev/null
@@ -0,0 +1,44 @@
+import difflib
+from distutils import dir_util
+import filecmp
+import os
+
+import click
+import pytest
+
+from lftools import cli
+
+
+FIXTURE_DIR = os.path.join(
+    os.path.dirname(os.path.realpath(__file__)),
+    'fixtures',
+    )
+
+
+@pytest.mark.datafiles(
+    os.path.join(FIXTURE_DIR, 'pom.xml'),
+    os.path.join(FIXTURE_DIR, 'pom.xml.expected-bump'),
+    )
+def test_version_bump(cli_runner, datafiles):
+    os.chdir(datafiles)
+
+    # Version bump should bump versions by x.(y+1).z
+    result = cli_runner.invoke(cli.cli, ['version', 'bump', 'TestRelease'])
+    assert filecmp.cmp('pom.xml', 'pom.xml.expected-bump')
+
+
+@pytest.mark.datafiles(
+    os.path.join(FIXTURE_DIR, 'pom.xml'),
+    os.path.join(FIXTURE_DIR, 'pom.xml.expected-release'),
+    os.path.join(FIXTURE_DIR, 'pom.xml.expected-release-bump'),
+    )
+def test_version_release(cli_runner, datafiles):
+    os.chdir(datafiles)
+
+    # Version release should modify SNAPSHOT to TestRelease
+    result = cli_runner.invoke(cli.cli, ['version', 'release', 'TestRelease'])
+    assert filecmp.cmp('pom.xml', 'pom.xml.expected-release')
+
+    # Post release bump should bump versions by x.y.(z+1) and revert back to SNAPSHOT
+    result = cli_runner.invoke(cli.cli, ['version', 'bump', 'TestRelease'])
+    assert filecmp.cmp('pom.xml', 'pom.xml.expected-release-bump')
diff --git a/tox.ini b/tox.ini
index 974a81a..e2fb390 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -3,7 +3,16 @@ minversion = 1.6
 envlist =
     coala,
     docs,
-    docs-linkcheck
+    docs-linkcheck,
+    py3
+
+[testenv]
+deps =
+    -r{toxinidir}/requirements.txt
+    pytest
+    pytest-click
+    pytest-datafiles
+commands = pytest
 
 [testenv:coala]
 basepython = python3