blob: 5a5a26c5e2df8221e7504a4164e3f620b4831cb2 [file] [log] [blame]
Mike Frysinger1b8565b2016-09-13 16:03:49 -04001# Copyright 2016 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Unittests for loman.py"""
6
7from __future__ import print_function
8
9import os
10
11from chromite.lib import cros_test_lib
12from chromite.lib import osutils
13from chromite.scripts import loman
14
15
16class ParserTest(cros_test_lib.OutputTestCase):
17 """Tests for the CLI parser."""
18
19 def setUp(self):
20 self.parser = loman.GetParser()
21
22 def testNoCommand(self):
23 """Require a command at least."""
24 with self.OutputCapturer():
25 self.assertRaises(SystemExit, self.parser.parse_args, [])
26
27 def testBadCommand(self):
28 """Reject unknown commands."""
29 with self.OutputCapturer():
30 self.assertRaises(SystemExit, self.parser.parse_args, ['flyaway'])
31
32 def testAddCommand(self):
33 """Verify basic add command behavior."""
34 with self.OutputCapturer():
35 self.parser.parse_args(['add', '--workon', 'project'])
36 self.parser.parse_args(['add', 'project', 'path', '--remote', 'foo'])
37
38 def testUpgradeMinilayoutCommand(self):
39 """Verify basic upgrade command behavior."""
40 with self.OutputCapturer():
41 self.parser.parse_args(['upgrade-minilayout'])
42
43
44class ManifestTest(cros_test_lib.TempDirTestCase):
45 """Tests that need a real .repo/ manifest layout."""
46
47 def setUp(self):
48 # The loman code looks for the repo root, so make one, and chdir there.
49 os.chdir(self.tempdir)
50
51 for d in ('repo', 'manifests', 'manifests.git'):
52 osutils.SafeMakedirs(os.path.join('.repo', d))
53
54 for m in ('default.xml', 'full.xml', 'minilayout.xml'):
55 osutils.Touch(os.path.join('.repo', 'manifests', m))
56
57 self._SetManifest('default.xml')
58
59 def _SetManifest(self, manifest):
60 """Set active manifest to point to |manifest|."""
61 source = os.path.join('.repo', 'manifest.xml')
62 target = os.path.join('.repo', 'manifests', manifest)
63 osutils.SafeUnlink(source)
64 os.symlink(target, source)
65
66
67class UpgradeMinilayoutTest(cros_test_lib.MockOutputTestCase, ManifestTest):
68 """Tests for the upgrade minilayout logic."""
69
70 def testNoUpgradeNeeded(self):
71 """Don't run update when it isn't needed."""
72 with self.OutputCapturer():
73 self.PatchObject(loman, '_UpgradeMinilayout', side_effect=Exception)
74 loman.main(['upgrade-minilayout'])
75
76 def testUpgradeNeeded(self):
77 """Run update when it's needed."""
78 class _Exception(Exception):
79 """Custom exception."""
80
81 self._SetManifest('minilayout.xml')
82 with self.OutputCapturer():
83 self.PatchObject(loman, '_UpgradeMinilayout', side_effect=_Exception)
84 self.assertRaises(_Exception, loman.main, ['upgrade-minilayout'])
85
86 def testAutoUpgrade(self):
87 """Run update automatically."""
88 class _Exception(Exception):
89 """Custom exception."""
90
91 self._SetManifest('minilayout.xml')
92 with self.OutputCapturer():
93 self.PatchObject(loman, '_UpgradeMinilayout', side_effect=_Exception)
94 self.assertRaises(_Exception, loman.main, ['add', '--workon', 'proj'])
95
96
97class AddTest(cros_test_lib.MockOutputTestCase, ManifestTest):
98 """Tests for the add command."""
99
100 def testRejectBadCommands(self):
101 """Reject bad invocations."""
102 bad_cmds = (
103 # Missing path.
104 ['add'],
105 # Extra project.
106 ['add', '--workon', 'path', 'project'],
107 # Missing --remote.
108 ['add', 'path', 'project'],
109 # Missing project.
110 ['add', 'path', '--remote', 'remote'],
111 )
112 with self.OutputCapturer():
113 for cmd in bad_cmds:
114 self.assertRaises(SystemExit, loman.main, cmd)