def testBoardAndDirectory(self):
    """Verify that "--board", "--test_results_root" are passed to the tests."""

    class AttributeTest(image_test.ForgivingImageTestCase):
      """Dummy test class to hold board and directory."""

      def testOkay(self):
        pass

    test = AttributeTest('testOkay')
    suite = image_test.ImageTestSuite()
    suite.addTest(test)
    self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
                     return_value=[suite])
    argv = [
        '--board',
        'my-board',
        '--test_results_root',
        'your-root',
        self.tempdir
    ]
    test_image.main(argv)
    # pylint: disable=W0212
    self.assertEqual('my-board', test._board)
    # pylint: disable=W0212
    self.assertEqual('your-root', os.path.basename(test._result_dir))
    def _testForgiveness(self, forgiveness, expected_result):
        class ForgivenessTest(image_test_lib.ImageTestCase):
            """A dummy test that is sometime forgiving, sometime not.

      Its only test (testFail) always fail.
      """

            _forgiving = True

            def SetForgiving(self, value):
                self._forgiving = value

            def IsForgiving(self):
                return self._forgiving

            def testFail(self):
                self.fail()

        test = ForgivenessTest('testFail')
        test.SetForgiving(forgiveness)
        suite = image_test_lib.ImageTestSuite()
        suite.addTest(test)
        self.PatchObject(unittest.TestLoader,
                         'loadTestsFromName',
                         autospec=True,
                         return_value=[suite])
        argv = [self.tempdir]
        self.assertEqual(expected_result, test_image.main(argv))
  def testChdir(self):
    """Verify the CWD is in a temp directory."""

    class CwdTest(image_test.NonForgivingImageTestCase):
      """A dummy test class to verify current working directory."""

      _expected_dir = None

      def SetCwd(self, cwd):
        self._expected_dir = cwd

      def testExpectedCwd(self):
        self.assertEqual(self._expected_dir, os.getcwd())

    self.assertNotEqual('/tmp', os.getcwd())
    os.chdir('/tmp')

    test = CwdTest('testExpectedCwd')
    suite = image_test.ImageTestSuite()
    suite.addTest(test)
    self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
                     return_value=[suite])

    # Set up the expected directory.
    expected_dir = os.path.join(self.tempdir, 'my-subdir')
    os.mkdir(expected_dir)
    test.SetCwd(expected_dir)
    self.PatchObject(tempfile, 'mkdtemp', autospec=True,
                     return_value=expected_dir)

    argv = [self.tempdir]
    self.assertEqual(0, test_image.main(argv))
    self.assertEqual('/tmp', os.getcwd())
  def _testForgiveness(self, forgiveness, expected_result):

    class ForgivenessTest(image_test.ImageTestCase):
      """A dummy test that is sometime forgiving, sometime not.

      Its only test (testFail) always fail.
      """

      _forgiving = True

      def SetForgiving(self, value):
        self._forgiving = value

      def IsForgiving(self):
        return self._forgiving

      def testFail(self):
        self.fail()

    test = ForgivenessTest('testFail')
    test.SetForgiving(forgiveness)
    suite = image_test.ImageTestSuite()
    suite.addTest(test)
    self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
                     return_value=[suite])
    argv = [self.tempdir]
    self.assertEqual(expected_result, test_image.main(argv))
    def testBoardAndDirectory(self):
        """Verify that "--board", "--test_results_root" are passed to the tests."""
        class AttributeTest(image_test_lib.ForgivingImageTestCase):
            """Dummy test class to hold board and directory."""
            def testOkay(self):
                pass

        test = AttributeTest('testOkay')
        suite = image_test_lib.ImageTestSuite()
        suite.addTest(test)
        self.PatchObject(unittest.TestLoader,
                         'loadTestsFromName',
                         autospec=True,
                         return_value=[suite])
        argv = [
            '--board', 'my-board', '--test_results_root', 'your-root',
            self.tempdir
        ]
        test_image.main(argv)
        # pylint: disable=W0212
        self.assertEqual('my-board', test._board)
        # pylint: disable=W0212
        self.assertEqual('your-root', os.path.basename(test._result_dir))
    def testChdir(self):
        """Verify the CWD is in a temp directory."""
        class CwdTest(image_test_lib.NonForgivingImageTestCase):
            """A dummy test class to verify current working directory."""

            _expected_dir = None

            def SetCwd(self, cwd):
                self._expected_dir = cwd

            def testExpectedCwd(self):
                self.assertEqual(self._expected_dir, os.getcwd())

        self.assertNotEqual('/tmp', os.getcwd())
        os.chdir('/tmp')

        test = CwdTest('testExpectedCwd')
        suite = image_test_lib.ImageTestSuite()
        suite.addTest(test)
        self.PatchObject(unittest.TestLoader,
                         'loadTestsFromName',
                         autospec=True,
                         return_value=[suite])

        # Set up the expected directory.
        expected_dir = os.path.join(self.tempdir, 'my-subdir')
        os.mkdir(expected_dir)
        test.SetCwd(expected_dir)
        self.PatchObject(tempfile,
                         'mkdtemp',
                         autospec=True,
                         return_value=expected_dir)

        argv = [self.tempdir]
        self.assertEqual(0, test_image.main(argv))
        self.assertEqual('/tmp', os.getcwd())