blob: 8b72b271212290bec3a1afb09bf9bacc35b5b795 [file] [log] [blame]
#!/usr/bin/python
# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unittests for chrome_update_manifest."""
import filecmp
import mox
import os
import re
import shutil
import sys
import tempfile
import unittest
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),
'..', '..'))
import chromite.lib.cros_build_lib as cros_lib
import chromite.bin.chrome_update_manifest as chrome_update_manifest
import chromite.buildbot.manifest_version as manifest_version
# pylint: disable=W0212,R0904
class DEPSFileTest(mox.MoxTestBase):
def setUp(self):
mox.MoxTestBase.setUp(self)
self.temp_manifest_dir = tempfile.mkdtemp()
print ('temp manifest dir: %s' % self.temp_manifest_dir)
self.manifest_path = os.path.join(self.temp_manifest_dir, 'oldlayout.xml')
self.tempdir = tempfile.mkdtemp()
self.test_base = os.path.join(os.path.realpath(os.path.dirname(__file__)),
'testdata/chrome_update_manifest_unittest')
self.repo_root = os.path.join(self.tempdir, 'repo_root')
self.repo_root_zip = os.path.join(self.test_base,
'repo_root.tar.gz')
assert(not os.path.exists(self.repo_root))
print('Unzipping test repo_root')
cros_lib.RunCommand(['tar', '-xvf', self.repo_root_zip],
cwd=self.tempdir, redirect_stdout=True)
assert(os.path.exists(self.repo_root))
self.old_dir = os.getcwd()
os.chdir(self.repo_root)
repo_root = cros_lib.FindRepoDir()
assert(os.path.realpath(os.path.dirname(repo_root)) == self.repo_root)
def _assertRaisesRegexp(self, regexp, func):
try:
func()
except Exception as e:
self.assertTrue(re.match(regexp, str(e)))
def _CopyTestFiles(self, test_name):
shutil.copyfile(os.path.join(self.test_base, test_name, 'DEPS.git'),
os.path.join(self.repo_root,
'chromium/src/.DEPS.git'))
internal_src_deps = os.path.join(self.test_base, test_name,
'DEPS_internal.git')
if os.path.exists(internal_src_deps):
shutil.copyfile(internal_src_deps,
os.path.join(self.repo_root,
'chromium/src-internal/.DEPS.git'))
shutil.copyfile(os.path.join(self.test_base, test_name, 'previous.xml'),
self.manifest_path)
def testParseInternalManifestWithDoubleCheckout(self):
"""Test a scenario where one project is checked out to two locations."""
self._CopyTestFiles('test1')
self.mox.StubOutWithMock(manifest_version, 'PrepForChanges')
manifest_version.PrepForChanges(mox.IgnoreArg(), False)
self.mox.ReplayAll()
manifest = chrome_update_manifest.Manifest(self.repo_root,
self.manifest_path,
'/does/not/exist',
internal=True, dryrun=True)
manifest.CreateNewManifest()
assert(filecmp.cmp(manifest.new_manifest_path,
os.path.join(self.test_base, 'test1/expected.xml'),
shallow=False))
def testNoBeginMarker(self):
"""Test case where manifest has no begin marker for chrome projects."""
self._CopyTestFiles('test2')
self.mox.StubOutWithMock(manifest_version, 'PrepForChanges')
manifest_version.PrepForChanges(mox.IgnoreArg(), False)
self.mox.ReplayAll()
manifest = chrome_update_manifest.Manifest(self.repo_root,
self.manifest_path,
'/does/not/exist',
internal=False, dryrun=True)
self._assertRaisesRegexp('.* begin marker .*',
manifest.CreateNewManifest)
def testNoEndMarker(self):
"""Test case where manifest has no end marker for chrome projects."""
self._CopyTestFiles('test3')
self.mox.StubOutWithMock(manifest_version, 'PrepForChanges')
manifest_version.PrepForChanges(mox.IgnoreArg(), False)
self.mox.ReplayAll()
manifest = chrome_update_manifest.Manifest(self.repo_root,
self.manifest_path,
'/does/not/exist',
internal=False, dryrun=True)
self._assertRaisesRegexp('.* end marker .*',
manifest.CreateNewManifest)
def testNonChromeProjectOverwritten(self):
"""Test case where """
self._CopyTestFiles('test4')
self.mox.StubOutWithMock(manifest_version, 'PrepForChanges')
manifest_version.PrepForChanges(mox.IgnoreArg(), False)
self.mox.ReplayAll()
manifest = chrome_update_manifest.Manifest(self.repo_root,
self.manifest_path,
'/does/not/exist',
internal=False, dryrun=True)
self._assertRaisesRegexp('.* accidentally .*',
manifest.CreateNewManifest)
def tearDown(self):
os.chdir(self.old_dir)
cros_lib.RunCommand(['rm', '-rf', self.tempdir, self.temp_manifest_dir])
assert(not os.path.exists(self.repo_root))
mox.MoxTestBase.tearDown(self)
if __name__ == '__main__':
unittest.main()