<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import unittest
import sys
import os
import copy
import cStringIO
import datetime

### DynamicConfig and its module should be a drop-in replacement for Config and its module
### The line below works, but it seemed overkill to duplicate the whole test so
### Just left this here as a reminder to play with it from time to time.
#import socorro.lib.ConfigurationManager as CM
import socorro.lib.dynamicConfigurationManager as CM

import optionfile

class HelpHandler:
  def __init__(self):
    self.data = ''
  def handleHelp(self, config):
    self.stringIO = cStringIO.StringIO()
    config.outputCommandSummary(self.stringIO,False)
    self.data = self.stringIO.getvalue()
    self.stringIO.close()

class TestConfigurationManager(unittest.TestCase):
  def __findConfigTstPath(self):
    if os.path.exists(os.path.join('.','config.tst')):
      return os.path.join('.','config.tst')
    # else
    dir = os.path.split(__file__)[0]
    return os.path.join(dir,'config.tst')
#     for dirpath,dirnames,filenames in os.walk("."):
#       if 'config.tst' in filenames:
#         return os.path.join(dirpath,'config.tst');
#     return os.path.join('.','config.tst') # this will give a nice failure message

  def setUp(self):
    self.keepargv = copy.copy(sys.argv)
    self.keepenviron = os.environ.copy()
    self.configTstPath = self.__findConfigTstPath()
    assert os.path.exists(self.configTstPath), "Why is this not in existence: %s"%(self.configTstPath)
    # all our tests depend on setting their own sys.argv AFTER the test has begun to run
    # In order to avoid trouble, blow away all the params that nosetests should 'use up'
    if 'nosetests' in sys.argv[0]:
      sys.argv = sys.argv[:1] # yes: Throw away all the params but the executable name

  def tearDown(self):
    sys.argv = copy.copy(self.keepargv)
    os.environ = self.keepenviron.copy()

  def testNewConfiguration(self):
    '''
    TestConfigurationManager.testNewConfiguration(self)
    Trick: To call f(**kwargs) with a dictionary d as the single arg, chant f(**d)
    '''
    opts = []
    args = {}
    args['automaticHelp'] = False

    # Test for empty
    conf = CM.newConfiguration(**args)
    assert(not conf.internal.allowableOptionDictionary)

    # Test for autoHelp
    args['automaticHelp'] = True<commit_msg>fixed test that patched but didn't restore os.environ<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import unittest
import sys
import os
import copy
import cStringIO
import datetime

### DynamicConfig and its module should be a drop-in replacement for Config and its module
### The line below works, but it seemed overkill to duplicate the whole test so
### Just left this here as a reminder to play with it from time to time.
#import socorro.lib.ConfigurationManager as CM
import socorro.lib.dynamicConfigurationManager as CM

import optionfile

class HelpHandler:
  def __init__(self):
    self.data = ''
  def handleHelp(self, config):
    self.stringIO = cStringIO.StringIO()
    config.outputCommandSummary(self.stringIO,False)
    self.data = self.stringIO.getvalue()
    self.stringIO.close()

class TestConfigurationManager(unittest.TestCase):
  def __findConfigTstPath(self):
    if os.path.exists(os.path.join('.','config.tst')):
      return os.path.join('.','config.tst')
    # else
    dir = os.path.split(__file__)[0]
    return os.path.join(dir,'config.tst')
#     for dirpath,dirnames,filenames in os.walk("."):
#       if 'config.tst' in filenames:
#         return os.path.join(dirpath,'config.tst');
#     return os.path.join('.','config.tst') # this will give a nice failure message

  def setUp(self):
    self.keepargv = copy.copy(sys.argv)
    self.keepenviron = os.environ
    self.configTstPath = self.__findConfigTstPath()
    assert os.path.exists(self.configTstPath), "Why is this not in existence: %s"%(self.configTstPath)
    # all our tests depend on setting their own sys.argv AFTER the test has begun to run
    # In order to avoid trouble, blow away all the params that nosetests should 'use up'
    if 'nosetests' in sys.argv[0]:
      sys.argv = sys.argv[:1] # yes: Throw away all the params but the executable name

  def tearDown(self):
    sys.argv = copy.copy(self.keepargv)
    os.environ = self.keepenviron

  def testNewConfiguration(self):
    '''
    TestConfigurationManager.testNewConfiguration(self)
    Trick: To call f(**kwargs) with a dictionary d as the single arg, chant f(**d)
    '''
    opts = []
    args = {}
    args['automaticHelp'] = False

    # Test for empty
    conf = CM.newConfiguration(**args)
    assert(not conf.internal.allowableOptionDictionary)

    # Test for autoHelp
    args['automaticHelp'] = True
{'fraction_non_alphanumeric': 0.10834321866350335, 'fraction_numerical': 0.00276789244760775, 'mean_word_length': 3.662672811059908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5993620', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1520, 'n_words': 504}