blob: 4865350e2c9a118cb5c80e7168a36e8402e88782 [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
Mike Frysinger1b8565b2016-09-13 16:03:49 -040038
39class ManifestTest(cros_test_lib.TempDirTestCase):
40 """Tests that need a real .repo/ manifest layout."""
41
42 def setUp(self):
43 # The loman code looks for the repo root, so make one, and chdir there.
44 os.chdir(self.tempdir)
45
46 for d in ('repo', 'manifests', 'manifests.git'):
47 osutils.SafeMakedirs(os.path.join('.repo', d))
48
49 for m in ('default.xml', 'full.xml', 'minilayout.xml'):
50 osutils.Touch(os.path.join('.repo', 'manifests', m))
51
52 self._SetManifest('default.xml')
53
54 def _SetManifest(self, manifest):
55 """Set active manifest to point to |manifest|."""
56 source = os.path.join('.repo', 'manifest.xml')
Gwendal Grignouf9d6d362016-09-30 09:29:20 -070057 target = os.path.join('manifests', manifest)
Mike Frysinger1b8565b2016-09-13 16:03:49 -040058 osutils.SafeUnlink(source)
59 os.symlink(target, source)
60
61
Mike Frysinger1b8565b2016-09-13 16:03:49 -040062class AddTest(cros_test_lib.MockOutputTestCase, ManifestTest):
63 """Tests for the add command."""
64
65 def testRejectBadCommands(self):
66 """Reject bad invocations."""
67 bad_cmds = (
68 # Missing path.
69 ['add'],
70 # Extra project.
71 ['add', '--workon', 'path', 'project'],
72 # Missing --remote.
73 ['add', 'path', 'project'],
74 # Missing project.
75 ['add', 'path', '--remote', 'remote'],
76 )
77 with self.OutputCapturer():
78 for cmd in bad_cmds:
79 self.assertRaises(SystemExit, loman.main, cmd)
Gwendal Grignouf9d6d362016-09-30 09:29:20 -070080
81
82class NoMiniayoutTest(cros_test_lib.MockOutputTestCase, ManifestTest):
83 """Check deprecated minilayout setups are detected."""
84
85 def setUp(self):
86 self._SetManifest('minilayout.xml')
87
88 def testMiniLayoutDetected(self):
89 """Check error is raised when repo is setup with minilayout."""
90
91 class _Error(Exception):
92 """Stub for test."""
93
94 self.PatchObject(loman, '_AssertNotMiniLayout', side_effect=_Error)
95 cmd = ['add', '-w', 'foo']
96 with self.OutputCapturer():
97 self.assertRaises(_Error, loman.main, cmd)