Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 1 | # 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 | |
| 7 | from __future__ import print_function |
| 8 | |
| 9 | import os |
| 10 | |
| 11 | from chromite.lib import cros_test_lib |
| 12 | from chromite.lib import osutils |
| 13 | from chromite.scripts import loman |
| 14 | |
| 15 | |
| 16 | class 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 Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 38 | |
| 39 | class 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 Grignou | f9d6d36 | 2016-09-30 09:29:20 -0700 | [diff] [blame^] | 57 | target = os.path.join('manifests', manifest) |
Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 58 | osutils.SafeUnlink(source) |
| 59 | os.symlink(target, source) |
| 60 | |
| 61 | |
Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 62 | class 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 Grignou | f9d6d36 | 2016-09-30 09:29:20 -0700 | [diff] [blame^] | 80 | |
| 81 | |
| 82 | class 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) |