| Index: recipe_engine/unittests/fetch_test.py
|
| diff --git a/recipe_engine/unittests/fetch_test.py b/recipe_engine/unittests/fetch_test.py
|
| new file mode 100755
|
| index 0000000000000000000000000000000000000000..88dbe4f7fe7142a3f92066f56047b6551cce2362
|
| --- /dev/null
|
| +++ b/recipe_engine/unittests/fetch_test.py
|
| @@ -0,0 +1,100 @@
|
| +#!/usr/bin/env python
|
| +# Copyright 2016 The Chromium Authors. All rights reserved.
|
| +# Use of this source code is governed by a BSD-style license that can be
|
| +# found in the LICENSE file.
|
| +
|
| +import os
|
| +import sys
|
| +import unittest
|
| +
|
| +BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(
|
| + os.path.abspath(__file__))))
|
| +THIRD_PARTY = os.path.join(BASE_DIR, 'recipe_engine', 'third_party')
|
| +sys.path.insert(0, os.path.join(THIRD_PARTY, 'mock-1.0.1'))
|
| +sys.path.insert(0, BASE_DIR)
|
| +sys.path.insert(0, THIRD_PARTY)
|
| +
|
| +import mock
|
| +import subprocess42
|
| +
|
| +from recipe_engine import fetch
|
| +
|
| +
|
| +class TestGit(unittest.TestCase):
|
| + @mock.patch('recipe_engine.fetch._run_git')
|
| + def test_fresh_clone(self, run_git):
|
| + fetch.fetch_from_git('repo', 'revision', 'dir', allow_fetch=True)
|
| + run_git.assert_has_calls([
|
| + mock.call(None, 'clone', '-q', 'repo', 'dir'),
|
| + mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'),
|
| + mock.call('dir', 'reset', '-q', '--hard', 'revision'),
|
| + ])
|
| +
|
| + @mock.patch('os.path.isdir')
|
| + @mock.patch('recipe_engine.fetch._run_git')
|
| + def test_existing_checkout(self, run_git, isdir):
|
| + isdir.return_value = True
|
| + fetch.fetch_from_git('repo', 'revision', 'dir', allow_fetch=True)
|
| + isdir.assert_has_calls([
|
| + mock.call('dir'),
|
| + mock.call('dir/.git'),
|
| + ])
|
| + run_git.assert_has_calls([
|
| + mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'),
|
| + mock.call('dir', 'reset', '-q', '--hard', 'revision'),
|
| + ])
|
| +
|
| + @mock.patch('recipe_engine.fetch._run_git')
|
| + def test_clone_not_allowed(self, run_git):
|
| + with self.assertRaises(fetch.FetchNotAllowedError):
|
| + fetch.fetch_from_git('repo', 'revision', 'dir', allow_fetch=False)
|
| +
|
| + @mock.patch('os.path.isdir')
|
| + @mock.patch('recipe_engine.fetch._run_git')
|
| + def test_unclean_filesystem(self, run_git, isdir):
|
| + isdir.side_effect = [True, False]
|
| + with self.assertRaises(fetch.UncleanFilesystemError):
|
| + fetch.fetch_from_git('repo', 'revision', 'dir', allow_fetch=False)
|
| + isdir.assert_has_calls([
|
| + mock.call('dir'),
|
| + mock.call('dir/.git'),
|
| + ])
|
| +
|
| + @mock.patch('os.path.isdir')
|
| + @mock.patch('recipe_engine.fetch._run_git')
|
| + def test_rev_parse_fail(self, run_git, isdir):
|
| + run_git.side_effect = [
|
| + subprocess42.CalledProcessError(1, ['fakecmd']),
|
| + None,
|
| + None,
|
| + ]
|
| + isdir.return_value = True
|
| + fetch.fetch_from_git('repo', 'revision', 'dir', allow_fetch=True)
|
| + isdir.assert_has_calls([
|
| + mock.call('dir'),
|
| + mock.call('dir/.git'),
|
| + ])
|
| + run_git.assert_has_calls([
|
| + mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'),
|
| + mock.call('dir', 'fetch'),
|
| + mock.call('dir', 'reset', '-q', '--hard', 'revision'),
|
| + ])
|
| +
|
| + @mock.patch('os.path.isdir')
|
| + @mock.patch('recipe_engine.fetch._run_git')
|
| + def test_rev_parse_fetch_not_allowed(self, run_git, isdir):
|
| + run_git.side_effect = [subprocess42.CalledProcessError(1, ['fakecmd'])]
|
| + isdir.return_value = True
|
| + with self.assertRaises(fetch.FetchNotAllowedError):
|
| + fetch.fetch_from_git('repo', 'revision', 'dir', allow_fetch=False)
|
| + isdir.assert_has_calls([
|
| + mock.call('dir'),
|
| + mock.call('dir/.git'),
|
| + ])
|
| + run_git.assert_has_calls([
|
| + mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'),
|
| + ])
|
| +
|
| +
|
| +if __name__ == '__main__':
|
| + unittest.main()
|
|
|