Repository: google/python-fire
File: fire/fire_test.py (Python)

Code before:

import fire
from fire import test_components as tc
from fire import testutils

import mock
import six


class FireTest(testutils.BaseTestCase):

  def testFire(self):
    with mock.patch.object(sys, 'argv', ['progname']):
      fire.Fire(tc.Empty)
      fire.Fire(tc.OldStyleEmpty)
      fire.Fire(tc.WithInit)
    self.assertEqual(fire.Fire(tc.NoDefaults, 'double 2'), 4)
    self.assertEqual(fire.Fire(tc.NoDefaults, 'triple 4'), 12)
    self.assertEqual(fire.Fire(tc.WithDefaults, 'double 2'), 4)
    self.assertEqual(fire.Fire(tc.WithDefaults, 'triple 4'), 12)
    self.assertEqual(fire.Fire(tc.OldStyleWithDefaults, 'double 2'), 4)
    self.assertEqual(fire.Fire(tc.OldStyleWithDefaults, 'triple 4'), 12)

  def testFireDefaultBaseName(self):
    with mock.patch.object(sys, 'argv',
                           ['/github/python-fire/fire/base_filename.py']):
      # positive case
      with self.assertOutputMatches(stdout='Usage:       base_filename.py',
                                    stderr=None):
        fire.Fire(tc.Empty)
      # negative case
      with self.assertRaises(AssertionError):
        with self.assertOutputMatches(stdout='Usage:fire/base_filename.py',
                                      stderr=None):
          fire.Fire(tc.Empty)

  def testFireNoArgs(self):
    self.assertEqual(fire.Fire(tc.MixedDefaults, 'ten'), 10)

  def testFireExceptions(self):
    # Exceptions of Fire are printed to stderr and a FireExit is raised.
    with self.assertRaisesFireExit(2):
      fire.Fire(tc.Empty, 'nomethod')  # Member doesn't exist.
    with self.assertRaisesFireExit(2):
      fire.Fire(tc.NoDefaults, 'double')  # Missing argument.
    with self.assertRaisesFireExit(2):
      fire.Fire(tc.TypedProperties, 'delta x')  # Missing key.

    # Exceptions of the target components are still raised.
    with self.assertRaises(ZeroDivisionError):
      fire.Fire(tc.NumberDefaults, 'reciprocal 0.0')

Review comment (suggestion):
Let's use something like `os.path.join('python-fire', 'fire', 'base_filename.py')` so that we don't assume unix vs windows (/ vs \\). [Also dropping 'github' from the path.] It's not important if it's an absolute or relative path, either's fine.

Code after:
import unittest

import fire
from fire import test_components as tc
from fire import testutils

import mock
import six


class FireTest(testutils.BaseTestCase):

  def testFire(self):
    with mock.patch.object(sys, 'argv', ['progname']):
      fire.Fire(tc.Empty)
      fire.Fire(tc.OldStyleEmpty)
      fire.Fire(tc.WithInit)
    self.assertEqual(fire.Fire(tc.NoDefaults, 'double 2'), 4)
    self.assertEqual(fire.Fire(tc.NoDefaults, 'triple 4'), 12)
    self.assertEqual(fire.Fire(tc.WithDefaults, 'double 2'), 4)
    self.assertEqual(fire.Fire(tc.WithDefaults, 'triple 4'), 12)
    self.assertEqual(fire.Fire(tc.OldStyleWithDefaults, 'double 2'), 4)
    self.assertEqual(fire.Fire(tc.OldStyleWithDefaults, 'triple 4'), 12)

  def testFireDefaultBaseName(self):
    with mock.patch.object(sys, 'argv',
                           [os.path.join('python-fire', 'fire',
                                         'base_filename.py')]):
      # positive case
      with self.assertOutputMatches(stdout='Usage:       base_filename.py',
                                    stderr=None):
        fire.Fire(tc.Empty)

  def testFireNoArgs(self):
    self.assertEqual(fire.Fire(tc.MixedDefaults, 'ten'), 10)

  def testFireExceptions(self):
    # Exceptions of Fire are printed to stderr and a FireExit is raised.
    with self.assertRaisesFireExit(2):
      fire.Fire(tc.Empty, 'nomethod')  # Member doesn't exist.
    with self.assertRaisesFireExit(2):
      fire.Fire(tc.NoDefaults, 'double')  # Missing argument.
    with self.assertRaisesFireExit(2):
      fire.Fire(tc.TypedProperties, 'delta x')  # Missing key.

    # Exceptions of the target components are still raised.
    with self.assertRaises(ZeroDivisionError):
      fire.Fire(tc.NumberDefaults, 'reciprocal 0.0')

  def testFireNamedArgs(self):
    self.assertEqual(fire.Fire(tc.WithDefaults, 'double --count 5'), 10)