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

Side by Side Diff: third_party/gsutil/gslib/tests/testcase/base.py

Issue 12042069: Scripts to download files from google storage based on sha1 sums (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Review fixes, updated gsutil Created 7 years, 10 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 import os.path
2 import random
3 import shutil
4 import tempfile
5
6 from gslib.tests.util import unittest
7
8
9 MAX_BUCKET_LENGTH = 63
10
11
12 class GsUtilTestCase(unittest.TestCase):
13 """Base test case class for unit and integration tests."""
14
15 def setUp(self):
16 self.tempdirs = []
17
18 def tearDown(self):
19 while self.tempdirs:
20 tmpdir = self.tempdirs.pop()
21 shutil.rmtree(tmpdir, ignore_errors=True)
22
23 def assertNumLines(self, text, numlines):
24 self.assertEqual(text.count('\n'), numlines)
25
26 def MakeTempName(self, kind):
27 """Creates a temporary name that is most-likely unique.
28
29 Args:
30 kind: A string indicating what kind of test name this is.
31
32 Returns:
33 The temporary name.
34 """
35 name = 'gsutil-test-%s-%s' % (self._testMethodName, kind)
36 name = name[:MAX_BUCKET_LENGTH-9]
37 name = '%s-%08x' % (name, random.randrange(256**4))
38 return name
39
40 def CreateTempDir(self, test_files=0):
41 """Creates a temporary directory on disk.
42
43 The directory and all of its contents will be deleted after the test.
44
45 Args:
46 test_files: The number of test files to place in the directory or a list
47 of test file names.
48
49 Returns:
50 The path to the new temporary directory.
51 """
52 tmpdir = tempfile.mkdtemp(prefix=self.MakeTempName('directory'))
53 self.tempdirs.append(tmpdir)
54 try:
55 iter(test_files)
56 except TypeError:
57 test_files = [self.MakeTempName('file') for _ in range(test_files)]
58 for i, name in enumerate(test_files):
59 self.CreateTempFile(tmpdir=tmpdir, file_name=name, contents='test %d' % i)
60 return tmpdir
61
62 def CreateTempFile(self, tmpdir=None, contents=None, file_name=None):
63 """Creates a temporary file on disk.
64
65 Args:
66 tmpdir: The temporary directory to place the file in. If not specified, a
67 new temporary directory is created.
68 file_name: The name to use for the file. If not specified, a temporary
69 test file name is constructed. This can also be a tuple, where
70 ('dir', 'foo') means to create a file named 'foo' inside a
71 subdirectory named 'dir'.
72 contents: The contents to write to the file. If not specified, a test
73 string is constructed and written to the file.
74
75 Returns:
76 The path to the new temporary file.
77 """
78 tmpdir = tmpdir or self.CreateTempDir()
79 file_name = file_name or self.MakeTempName('file')
80 if isinstance(file_name, basestring):
81 fpath = os.path.join(tmpdir, file_name)
82 else:
83 fpath = os.path.join(tmpdir, *file_name)
84 if not os.path.isdir(os.path.dirname(fpath)):
85 os.makedirs(os.path.dirname(fpath))
86 with open(fpath, 'w') as f:
87 contents = contents or self.MakeTempName('contents')
88 f.write(contents)
89 return fpath
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698