Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(870)

Unified Diff: recipe_engine/unittests/fetch_test.py

Issue 1997023002: recipe engine: add remote_run command (Closed) Base URL: https://github.com/luci/recipes-py.git@master
Patch Set: trybots Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « recipe_engine/remote_run.py ('k') | recipes.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..0bd96aea8a3506043d8d4f6beebfb50472b37de1
--- /dev/null
+++ b/recipe_engine/unittests/fetch_test.py
@@ -0,0 +1,134 @@
+#!/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):
+ run_git.side_effect = [
+ None,
+ 'repo\n',
+ None,
+ None,
+ ]
+ fetch.ensure_git_checkout('repo', 'revision', 'dir', allow_fetch=True)
+ run_git.assert_has_calls([
+ mock.call(None, 'clone', '-q', 'repo', 'dir'),
+ mock.call('dir', 'config', 'remote.origin.url'),
+ 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):
+ run_git.side_effect = [
+ 'repo\n',
+ None,
+ None,
+ ]
+ isdir.return_value = True
+ fetch.ensure_git_checkout('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', 'config', 'remote.origin.url'),
+ 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.ensure_git_checkout('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.ensure_git_checkout('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_origin_mismatch(self, run_git, isdir):
+ run_git.return_value = 'not-repo'
+ isdir.return_value = True
+ with self.assertRaises(fetch.UncleanFilesystemError):
+ fetch.ensure_git_checkout('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', 'config', 'remote.origin.url'),
+ ])
+
+ @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 = [
+ 'repo',
+ subprocess42.CalledProcessError(1, ['fakecmd']),
+ None,
+ None,
+ ]
+ isdir.return_value = True
+ fetch.ensure_git_checkout('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', 'config', 'remote.origin.url'),
+ 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 = [
+ 'repo',
+ subprocess42.CalledProcessError(1, ['fakecmd']),
+ ]
+ isdir.return_value = True
+ with self.assertRaises(fetch.FetchNotAllowedError):
+ fetch.ensure_git_checkout('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', 'config', 'remote.origin.url'),
+ mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'),
+ ])
+
+
+if __name__ == '__main__':
+ unittest.main()
« no previous file with comments | « recipe_engine/remote_run.py ('k') | recipes.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698