Merge lp:~thomir-deactivatedaccount/autopilot/add-version-option into lp:autopilot

Proposed by Thomi Richards
Status: Merged
Approved by: Christopher Lee
Approved revision: 225
Merged at revision: 224
Proposed branch: lp:~thomir-deactivatedaccount/autopilot/add-version-option
Merge into: lp:autopilot
Diff against target: 153 lines (+127/-0)
2 files modified
autopilot/__init__.py (+51/-0)
autopilot/tests/unit/test_version_utility_fns.py (+76/-0)
To merge this branch: bzr merge lp:~thomir-deactivatedaccount/autopilot/add-version-option
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve
Christopher Lee (community) Approve
Review via email: mp+165940@code.launchpad.net

Commit message

Add a -v, --version command line option.

Description of the change

Add the -v, --version command for autopilot.

To post a comment you must log in.
Revision history for this message
Christopher Lee (veebers) wrote :

Looks good

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'autopilot/__init__.py'
2--- autopilot/__init__.py 2013-05-24 04:54:34 +0000
3+++ autopilot/__init__.py 2013-05-27 22:01:28 +0000
4@@ -18,6 +18,7 @@
5 #
6
7 from argparse import ArgumentParser, REMAINDER, Action
8+import subprocess
9
10 version = '1.3'
11
12@@ -39,6 +40,9 @@
13 object.
14 """
15 parser = ArgumentParser(description="Autopilot test tool.")
16+ parser.add_argument('-v', '--version', action='version',
17+ version=get_version_string(),
18+ help="Display autopilot version and exit.")
19 subparsers = parser.add_subparsers(help='Run modes', dest="mode")
20
21 parser_run = subparsers.add_parser('run', help="Run autopilot tests")
22@@ -126,3 +130,50 @@
23 return True
24 except ImportError:
25 return False
26+
27+
28+def get_version_string():
29+ version_string = "Autopilot Source Version: " + _get_source_version()
30+ pkg_version = _get_package_version()
31+ if pkg_version:
32+ version_string += "\nAutopilot Package Version: " + pkg_version
33+ return version_string
34+
35+
36+def _get_source_version():
37+ return version
38+
39+
40+def _get_package_version():
41+ """Get the version of the currently installed package version, or None.
42+
43+ Only returns the package version if the package is installed, *and* we seem
44+ to be running the system-wide installed code.
45+ """
46+ if _running_in_system():
47+ return _get_package_installed_version()
48+ return None
49+
50+
51+def _running_in_system():
52+ """Return True if we're running autopilot from the system installation dir."""
53+ return __file__.startswith('/usr/')
54+
55+
56+def _get_package_installed_version():
57+ """Get the version string of the system-wide installed package, or None if it
58+ is not installed.
59+
60+ """
61+ try:
62+ return subprocess.check_output(
63+ [
64+ "dpkg-query",
65+ "--showformat",
66+ "${Version}",
67+ "--show",
68+ "python-autopilot",
69+ ]
70+ ).strip()
71+ except subprocess.CalledProcessError:
72+ return None
73
74=== added file 'autopilot/tests/unit/test_version_utility_fns.py'
75--- autopilot/tests/unit/test_version_utility_fns.py 1970-01-01 00:00:00 +0000
76+++ autopilot/tests/unit/test_version_utility_fns.py 2013-05-27 22:01:28 +0000
77@@ -0,0 +1,76 @@
78+# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
79+#
80+# Autopilot Functional Test Tool
81+# Copyright (C) 2012-2013 Canonical
82+#
83+# This program is free software: you can redistribute it and/or modify
84+# it under the terms of the GNU General Public License as published by
85+# the Free Software Foundation, either version 3 of the License, or
86+# (at your option) any later version.
87+#
88+# This program is distributed in the hope that it will be useful,
89+# but WITHOUT ANY WARRANTY; without even the implied warranty of
90+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
91+# GNU General Public License for more details.
92+#
93+# You should have received a copy of the GNU General Public License
94+# along with this program. If not, see <http://www.gnu.org/licenses/>.
95+#
96+
97+from mock import patch
98+import subprocess
99+from testtools import TestCase
100+from testtools.matchers import Equals
101+
102+from autopilot import (
103+ _get_package_installed_version,
104+ _get_package_version,
105+ get_version_string,
106+ )
107+
108+class VersionFnTests(TestCase):
109+
110+ def test_package_version_returns_none_when_running_from_source(self):
111+ """_get_package_version must return None if we're not running in the
112+ system.
113+
114+ """
115+ with patch('autopilot._running_in_system', new=lambda: False):
116+ self.assertThat(_get_package_version(), Equals(None))
117+
118+ def test_get_package_installed_version_returns_None_on_error(self):
119+ """The _get_package_installed_version function must return None when
120+ subprocess raises an error while calling dpkg-query.
121+ """
122+ def raise_error(*args):
123+ raise subprocess.CalledProcessError(1, "dpkg-query")
124+ with patch('subprocess.check_output', new=raise_error):
125+ self.assertThat(_get_package_installed_version(), Equals(None))
126+
127+ def test_get_package_installed_version_strips_command_output(self):
128+ """The _get_package_installed_version function must strip the output of
129+ the dpkg-query function.
130+
131+ """
132+ with patch('subprocess.check_output', new=lambda *a: "1.3daily13.05.22\n"):
133+ self.assertThat(_get_package_installed_version(), Equals("1.3daily13.05.22"))
134+
135+ def test_get_version_string_shows_source_version(self):
136+ """The get_version_string function must only show the source version if
137+ the system version returns None.
138+
139+ """
140+ with patch('autopilot._get_package_version', new=lambda: None):
141+ with patch('autopilot._get_source_version', new=lambda: "1.3.1"):
142+ version_string = get_version_string()
143+ self.assertThat(version_string, Equals("Autopilot Source Version: 1.3.1"))
144+
145+ def test_get_version_string_shows_both_versions(self):
146+ """The get_version_string function must show both source and package
147+ versions, when the package version is avaialble.capitalize
148+ """
149+ with patch('autopilot._get_package_version', new=lambda: "1.3.1daily13.05.22"):
150+ with patch('autopilot._get_source_version', new=lambda: "1.3.1"):
151+ version_string = get_version_string()
152+ self.assertThat(version_string,
153+ Equals("Autopilot Source Version: 1.3.1\nAutopilot Package Version: 1.3.1daily13.05.22"))

Subscribers

People subscribed via source and target branches