OLD | NEW |
1 # Copyright 2012 The Chromium Authors. All rights reserved. | 1 # Copyright 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Provides stubs for os, sys and subprocess for testing | 5 """Provides stubs for os, sys and subprocess for testing |
6 | 6 |
7 This test allows one to test code that itself uses os, sys, and subprocess. | 7 This test allows one to test code that itself uses os, sys, and subprocess. |
8 """ | 8 """ |
9 | 9 |
10 import os | 10 import os |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 result = CloudStorageModuleStub.GetHelper( | 284 result = CloudStorageModuleStub.GetHelper( |
285 self, self.PUBLIC_BUCKET, remote_path, local_path, True) | 285 self, self.PUBLIC_BUCKET, remote_path, local_path, True) |
286 if not result: | 286 if not result: |
287 result = CloudStorageModuleStub.GetHelper( | 287 result = CloudStorageModuleStub.GetHelper( |
288 self, self.PARTNER_BUCKET, remote_path, local_path, True) | 288 self, self.PARTNER_BUCKET, remote_path, local_path, True) |
289 if not result: | 289 if not result: |
290 result = CloudStorageModuleStub.GetHelper( | 290 result = CloudStorageModuleStub.GetHelper( |
291 self, self.INTERNAL_BUCKET, remote_path, local_path, True) | 291 self, self.INTERNAL_BUCKET, remote_path, local_path, True) |
292 return result | 292 return result |
293 | 293 |
294 def GetFilesInDirectoryIfChanged(self, directory, bucket): | |
295 if os.path.dirname(directory) == directory: # If in the root dir. | |
296 raise ValueError('Trying to serve root directory from HTTP server.') | |
297 for dirpath, _, filenames in os.walk(directory): | |
298 for filename in filenames: | |
299 path, extension = os.path.splitext( | |
300 os.path.join(dirpath, filename)) | |
301 if extension != '.sha1': | |
302 continue | |
303 self.GetIfChanged(path, bucket) | |
304 | |
305 def CalculateHash(self, file_path): | 294 def CalculateHash(self, file_path): |
306 return self.local_file_hashes[file_path] | 295 return self.local_file_hashes[file_path] |
307 | 296 |
308 def ReadHash(self, hash_path): | 297 def ReadHash(self, hash_path): |
309 return self.local_hash_files[hash_path] | 298 return self.local_hash_files[hash_path] |
310 | 299 |
311 | 300 |
312 class LoggingStub(object): | 301 class LoggingStub(object): |
313 def __init__(self): | 302 def __init__(self): |
314 self.warnings = [] | 303 self.warnings = [] |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 | 348 |
360 class OsModuleStub(object): | 349 class OsModuleStub(object): |
361 class OsEnvironModuleStub(object): | 350 class OsEnvironModuleStub(object): |
362 def get(self, _): | 351 def get(self, _): |
363 return None | 352 return None |
364 | 353 |
365 class OsPathModuleStub(object): | 354 class OsPathModuleStub(object): |
366 def __init__(self, sys_module): | 355 def __init__(self, sys_module): |
367 self.sys = sys_module | 356 self.sys = sys_module |
368 self.files = [] | 357 self.files = [] |
369 self.dirs = [] | |
370 | 358 |
371 def exists(self, path): | 359 def exists(self, path): |
372 return path in self.files | 360 return path in self.files |
373 | 361 |
374 def isfile(self, path): | 362 def isfile(self, path): |
375 return path in self.files | 363 return path in self.files |
376 | 364 |
377 def isdir(self, path): | |
378 return path in self.dirs | |
379 | |
380 def join(self, *paths): | 365 def join(self, *paths): |
381 def IsAbsolutePath(path): | 366 def IsAbsolutePath(path): |
382 if self.sys.platform.startswith('win'): | 367 if self.sys.platform.startswith('win'): |
383 return re.match('[a-zA-Z]:\\\\', path) | 368 return re.match('[a-zA-Z]:\\\\', path) |
384 else: | 369 else: |
385 return path.startswith('/') | 370 return path.startswith('/') |
386 | 371 |
387 # Per Python specification, if any component is an absolute path, | 372 # Per Python specification, if any component is an absolute path, |
388 # discard previous components. | 373 # discard previous components. |
389 for index, path in reversed(list(enumerate(paths))): | 374 for index, path in reversed(list(enumerate(paths))): |
390 if IsAbsolutePath(path): | 375 if IsAbsolutePath(path): |
391 paths = paths[index:] | 376 paths = paths[index:] |
392 break | 377 break |
393 | 378 |
394 if self.sys.platform.startswith('win'): | 379 if self.sys.platform.startswith('win'): |
395 tmp = os.path.join(*paths) | 380 tmp = os.path.join(*paths) |
396 return tmp.replace('/', '\\') | 381 return tmp.replace('/', '\\') |
397 else: | 382 else: |
398 tmp = os.path.join(*paths) | 383 tmp = os.path.join(*paths) |
399 return tmp.replace('\\', '/') | 384 return tmp.replace('\\', '/') |
400 | 385 |
401 @staticmethod | 386 @staticmethod |
402 def abspath(path): | |
403 return os.path.abspath(path) | |
404 | |
405 @staticmethod | |
406 def expanduser(path): | 387 def expanduser(path): |
407 return os.path.expanduser(path) | 388 return os.path.expanduser(path) |
408 | 389 |
409 @staticmethod | 390 @staticmethod |
410 def dirname(path): | 391 def dirname(path): |
411 return os.path.dirname(path) | 392 return os.path.dirname(path) |
412 | 393 |
413 @staticmethod | 394 @staticmethod |
414 def splitext(path): | 395 def splitext(path): |
415 return os.path.splitext(path) | 396 return os.path.splitext(path) |
416 | 397 |
417 @staticmethod | |
418 def splitdrive(path): | |
419 return os.path.splitdrive(path) | |
420 | |
421 X_OK = os.X_OK | 398 X_OK = os.X_OK |
422 | 399 |
423 sep = os.sep | |
424 pathsep = os.pathsep | 400 pathsep = os.pathsep |
425 | 401 |
426 def __init__(self, sys_module=sys): | 402 def __init__(self, sys_module=sys): |
427 self.path = OsModuleStub.OsPathModuleStub(sys_module) | 403 self.path = OsModuleStub.OsPathModuleStub(sys_module) |
428 self.environ = OsModuleStub.OsEnvironModuleStub() | 404 self.environ = OsModuleStub.OsEnvironModuleStub() |
429 self.display = ':0' | 405 self.display = ':0' |
430 self.local_app_data = None | 406 self.local_app_data = None |
431 self.sys_path = None | 407 self.sys_path = None |
432 self.program_files = None | 408 self.program_files = None |
433 self.program_files_x86 = None | 409 self.program_files_x86 = None |
434 self.devnull = os.devnull | 410 self.devnull = os.devnull |
435 self._directory = {} | |
436 | 411 |
437 def access(self, path, _): | 412 def access(self, path, _): |
438 return path in self.path.files | 413 return path in self.path.files |
439 | 414 |
440 def getenv(self, name, value=None): | 415 def getenv(self, name, value=None): |
441 if name == 'DISPLAY': | 416 if name == 'DISPLAY': |
442 env = self.display | 417 env = self.display |
443 elif name == 'LOCALAPPDATA': | 418 elif name == 'LOCALAPPDATA': |
444 env = self.local_app_data | 419 env = self.local_app_data |
445 elif name == 'PATH': | 420 elif name == 'PATH': |
446 env = self.sys_path | 421 env = self.sys_path |
447 elif name == 'PROGRAMFILES': | 422 elif name == 'PROGRAMFILES': |
448 env = self.program_files | 423 env = self.program_files |
449 elif name == 'PROGRAMFILES(X86)': | 424 elif name == 'PROGRAMFILES(X86)': |
450 env = self.program_files_x86 | 425 env = self.program_files_x86 |
451 else: | 426 else: |
452 raise NotImplementedError('Unsupported getenv') | 427 raise NotImplementedError('Unsupported getenv') |
453 return env if env else value | 428 return env if env else value |
454 | 429 |
455 def chdir(self, path): | 430 def chdir(self, path): |
456 pass | 431 pass |
457 | 432 |
458 def walk(self, top): | |
459 for dir_name in self._directory: | |
460 yield top, dir_name, self._directory[dir_name] | |
461 | |
462 | 433 |
463 class PerfControlModuleStub(object): | 434 class PerfControlModuleStub(object): |
464 class PerfControlStub(object): | 435 class PerfControlStub(object): |
465 def __init__(self, adb): | 436 def __init__(self, adb): |
466 pass | 437 pass |
467 | 438 |
468 def __init__(self): | 439 def __init__(self): |
469 self.PerfControl = PerfControlModuleStub.PerfControlStub | 440 self.PerfControl = PerfControlModuleStub.PerfControlStub |
470 | 441 |
471 | 442 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 class AdbInstallCertStub(object): | 503 class AdbInstallCertStub(object): |
533 class AndroidCertInstaller(object): | 504 class AndroidCertInstaller(object): |
534 def __init__(self, device_id, _cert_name, _cert_path): | 505 def __init__(self, device_id, _cert_name, _cert_path): |
535 if device_id == 'success': | 506 if device_id == 'success': |
536 pass | 507 pass |
537 elif device_id == 'failure': | 508 elif device_id == 'failure': |
538 raise Exception('Test exception.') | 509 raise Exception('Test exception.') |
539 | 510 |
540 def install_cert(self, overwrite_cert=False): | 511 def install_cert(self, overwrite_cert=False): |
541 pass | 512 pass |
OLD | NEW |