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

Side by Side 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: 80cols 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 unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright 2016 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import os
7 import sys
8 import unittest
9
10 BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(
11 os.path.abspath(__file__))))
12 THIRD_PARTY = os.path.join(BASE_DIR, 'recipe_engine', 'third_party')
13 sys.path.insert(0, os.path.join(THIRD_PARTY, 'mock-1.0.1'))
14 sys.path.insert(0, BASE_DIR)
15 sys.path.insert(0, THIRD_PARTY)
16
17 import mock
18 import subprocess42
19
20 from recipe_engine import fetch
21
22
23 class TestGit(unittest.TestCase):
24 @mock.patch('recipe_engine.fetch._run_git')
25 def test_fresh_clone(self, run_git):
26 fetch.fetch_from_git('repo', 'revision', 'dir', allow_fetch=True)
27 run_git.assert_has_calls([
28 mock.call(None, 'clone', '-q', 'repo', 'dir'),
29 mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'),
30 mock.call('dir', 'reset', '-q', '--hard', 'revision'),
31 ])
32
33 @mock.patch('os.path.isdir')
34 @mock.patch('recipe_engine.fetch._run_git')
35 def test_existing_checkout(self, run_git, isdir):
36 isdir.return_value = True
37 fetch.fetch_from_git('repo', 'revision', 'dir', allow_fetch=True)
38 isdir.assert_has_calls([
39 mock.call('dir'),
40 mock.call('dir/.git'),
41 ])
42 run_git.assert_has_calls([
43 mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'),
44 mock.call('dir', 'reset', '-q', '--hard', 'revision'),
45 ])
46
47 @mock.patch('recipe_engine.fetch._run_git')
48 def test_clone_not_allowed(self, run_git):
49 with self.assertRaises(fetch.FetchNotAllowedError):
50 fetch.fetch_from_git('repo', 'revision', 'dir', allow_fetch=False)
51
52 @mock.patch('os.path.isdir')
53 @mock.patch('recipe_engine.fetch._run_git')
54 def test_unclean_filesystem(self, run_git, isdir):
55 isdir.side_effect = [True, False]
56 with self.assertRaises(fetch.UncleanFilesystemError):
57 fetch.fetch_from_git('repo', 'revision', 'dir', allow_fetch=False)
58 isdir.assert_has_calls([
59 mock.call('dir'),
60 mock.call('dir/.git'),
61 ])
62
63 @mock.patch('os.path.isdir')
64 @mock.patch('recipe_engine.fetch._run_git')
65 def test_rev_parse_fail(self, run_git, isdir):
66 run_git.side_effect = [
67 subprocess42.CalledProcessError(1, ['fakecmd']),
68 None,
69 None,
70 ]
71 isdir.return_value = True
72 fetch.fetch_from_git('repo', 'revision', 'dir', allow_fetch=True)
73 isdir.assert_has_calls([
74 mock.call('dir'),
75 mock.call('dir/.git'),
76 ])
77 run_git.assert_has_calls([
78 mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'),
79 mock.call('dir', 'fetch'),
80 mock.call('dir', 'reset', '-q', '--hard', 'revision'),
81 ])
82
83 @mock.patch('os.path.isdir')
84 @mock.patch('recipe_engine.fetch._run_git')
85 def test_rev_parse_fetch_not_allowed(self, run_git, isdir):
86 run_git.side_effect = [subprocess42.CalledProcessError(1, ['fakecmd'])]
87 isdir.return_value = True
88 with self.assertRaises(fetch.FetchNotAllowedError):
89 fetch.fetch_from_git('repo', 'revision', 'dir', allow_fetch=False)
90 isdir.assert_has_calls([
91 mock.call('dir'),
92 mock.call('dir/.git'),
93 ])
94 run_git.assert_has_calls([
95 mock.call('dir', 'rev-parse', '-q', '--verify', 'revision^{commit}'),
96 ])
97
98
99 if __name__ == '__main__':
100 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698