<commit_before>#!/usr/bin/env python
# Copyright (c) 2012 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.

"""Tests for module gs_utils."""

import __builtin__
import os
import sys
import tempfile
import time

# Appending to PYTHONPATH to find common.
buildbot_path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                             os.pardir, os.pardir, os.pardir)
sys.path.append(os.path.join(buildbot_path, 'third_party', 'chromium_buildbot',
                             'scripts'))
sys.path.append(os.path.join(buildbot_path, 'third_party', 'chromium_buildbot',
                             'scripts', 'common'))
sys.path.append(os.path.join(buildbot_path, 'third_party', 'chromium_buildbot',
                             'site_config'))

import chromium_utils
from slave import slave_utils
import gs_utils
import unittest


GSUTIL_LOCATION = os.path.join(
    os.pardir, os.pardir, 'third_party', 'chromium_buildbot', 'scripts',
    'slave', 'gsutil')

TEST_TIMESTAMP = '1354128965'
TEST_TIMESTAMP_2 = '1354128985'


class TestGSUtils(unittest.TestCase):

  def setUp(self):
    self._expected_command = None
    self._test_temp_file = None
    self._test_gs_base = None
    self._test_destdir = None
    self._test_gs_acl = None

    def _MockCommand(command):
      self.assertEquals(self._expected_command, ' '.join(command))

    def _MockGSUtilFileCopy(filename, gs_base, subdir, gs_acl):
      self.assertEquals(self._test_temp_file, filename)
      self.assertEquals(self._test_gs_base, gs_base)
      self.assertEquals(self._test_destdir, subdir)
      self.assertEquals(self._test_gs_acl, gs_acl)

    def _MockGSUtilDownloadFile(src, dst):
      pass

    self._original_run_command = chromium_utils.RunCommand
    chromium_utils.RunCommand = _MockCommand

    self._original_gsutil_file_copy = slave_utils.GSUtilCopyFile
    slave_utils.GSUtilCopyFile = _MockGSUtilFileCopy
    
    self._original_gsutil_download_file = slave_utils.GSUtilDownloadFile
    slave_utils.GSUtilDownloadFile = _MockGSUtilDownloadFile

    self._original_file = __builtin__.open

  def tearDown(self):
    chromium_utils.RunCommand = self._original_run_command
    slave_utils.GSUtilCopyFile = self._original_gsutil_file_copy
    slave_utils.GSUtilDownloadFile = self._original_gsutil_download_file
    __builtin__.open = self._original_file

  def test_DeleteStorageObject(self):
    self._expected_command = ('%s rm -R superman' % GSUTIL_LOCATION)
    gs_utils.DeleteStorageObject('superman')

  # TODO(rmistry): Disabled in https://codereview.chromium.org/17473003 ,
  # because it wasn't clear how to make it work with shell_utils.Bash()
  # instead of chromium_utils.RunCommand().
  def DISABLEDtestCopyStorageDirectory(self):
    self._expected_command = (<commit_msg>Re-enable disabled test in gs_utils_test<commit_after>#!/usr/bin/env python
# Copyright (c) 2012 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.

"""Tests for module gs_utils."""

import __builtin__
import os
import shell_utils
import sys
import tempfile
import time

# Appending to PYTHONPATH to find common.
buildbot_path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                             os.pardir, os.pardir, os.pardir)
sys.path.append(os.path.join(buildbot_path, 'third_party', 'chromium_buildbot',
                             'scripts'))
sys.path.append(os.path.join(buildbot_path, 'third_party', 'chromium_buildbot',
                             'scripts', 'common'))
sys.path.append(os.path.join(buildbot_path, 'third_party', 'chromium_buildbot',
                             'site_config'))

import chromium_utils
from slave import slave_utils
import gs_utils
import unittest


GSUTIL_LOCATION = slave_utils.GSUtilSetup()


TEST_TIMESTAMP = '1354128965'
TEST_TIMESTAMP_2 = '1354128985'


class TestGSUtils(unittest.TestCase):

  def setUp(self):
    self._expected_command = None
    self._test_temp_file = None
    self._test_gs_base = None
    self._test_destdir = None
    self._test_gs_acl = None

    def _MockCommand(command):
      self.assertEquals(self._expected_command, ' '.join(command))

    def _MockGSUtilFileCopy(filename, gs_base, subdir, gs_acl):
      self.assertEquals(self._test_temp_file, filename)
      self.assertEquals(self._test_gs_base, gs_base)
      self.assertEquals(self._test_destdir, subdir)
      self.assertEquals(self._test_gs_acl, gs_acl)

    def _MockGSUtilDownloadFile(src, dst):
      pass

    self._original_run_command = chromium_utils.RunCommand
    chromium_utils.RunCommand = _MockCommand

    self._original_bash_run_command = shell_utils.Bash
    shell_utils.Bash = _MockCommand

    self._original_gsutil_file_copy = slave_utils.GSUtilCopyFile
    slave_utils.GSUtilCopyFile = _MockGSUtilFileCopy
    
    self._original_gsutil_download_file = slave_utils.GSUtilDownloadFile
    slave_utils.GSUtilDownloadFile = _MockGSUtilDownloadFile

    self._original_file = __builtin__.open

  def tearDown(self):
    chromium_utils.RunCommand = self._original_run_command
    shell_utils.Bash = self._original_bash_run_command
    slave_utils.GSUtilCopyFile = self._original_gsutil_file_copy
    slave_utils.GSUtilDownloadFile = self._original_gsutil_download_file
    __builtin__.open = self._original_file

  def test_DeleteStorageObject(self):
    self._expected_command = ('%s rm -R superman' % GSUTIL_LOCATION)
    gs_utils.DeleteStorageObject('superman')

  def test_CopyStorageDirectory(self):
    self._expected_command = (
{'fraction_non_alphanumeric': 0.08774397749252681, 'fraction_numerical': 0.010198698786706524, 'mean_word_length': 3.9807355516637477, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4732249', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1817, 'n_words': 347}