"Fossies" - the Fresh Open Source Software Archive

Member "http-prompt-2.1.0/tests/test_config.py" (5 Mar 2021, 2237 Bytes) of package /linux/www/http-prompt-2.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 import hashlib
    2 import os
    3 
    4 from .base import TempAppDirTestCase
    5 from http_prompt import config
    6 
    7 
    8 def _hash_file(path):
    9     with open(path, 'rb') as f:
   10         data = f.read()
   11     return hashlib.sha1(data).hexdigest()
   12 
   13 
   14 class TestConfig(TempAppDirTestCase):
   15 
   16     def test_initialize(self):
   17         # Config file doesn't exist at first
   18         expected_path = config.get_user_config_path()
   19         self.assertFalse(os.path.exists(expected_path))
   20 
   21         # Config file should exist after initialization
   22         copied, actual_path = config.initialize()
   23         self.assertTrue(copied)
   24         self.assertEqual(actual_path, expected_path)
   25         self.assertTrue(os.path.exists(expected_path))
   26 
   27         # Change config file and hash the content to see if it's changed
   28         with open(expected_path, 'a') as f:
   29             f.write('dont_care\n')
   30         orig_hash = _hash_file(expected_path)
   31 
   32         # Make sure it's fine to call config.initialize() twice
   33         copied, actual_path = config.initialize()
   34         self.assertFalse(copied)
   35         self.assertEqual(actual_path, expected_path)
   36         self.assertTrue(os.path.exists(expected_path))
   37 
   38         # Make sure config file is unchanged
   39         new_hash = _hash_file(expected_path)
   40         self.assertEqual(new_hash, orig_hash)
   41 
   42     def test_load_default(self):
   43         cfg = config.load_default()
   44         self.assertEqual(cfg['command_style'], 'solarized')
   45         self.assertFalse(cfg['output_style'])
   46         self.assertEqual(cfg['pager'], 'less')
   47 
   48     def test_load_user(self):
   49         copied, path = config.initialize()
   50         self.assertTrue(copied)
   51 
   52         with open(path, 'w') as f:
   53             f.write("\ngreeting = 'hello!'\n")
   54 
   55         cfg = config.load_user()
   56         self.assertEqual(cfg, {'greeting': 'hello!'})
   57 
   58     def test_load(self):
   59         copied, path = config.initialize()
   60         self.assertTrue(copied)
   61 
   62         with open(path, 'w') as f:
   63             f.write("pager = 'more'\n"
   64                     "greeting = 'hello!'\n")
   65 
   66         cfg = config.load()
   67         self.assertEqual(cfg['command_style'], 'solarized')
   68         self.assertFalse(cfg['output_style'])
   69         self.assertEqual(cfg['pager'], 'more')
   70         self.assertEqual(cfg['greeting'], 'hello!')