~jaap.karssenberg/zim/pyzim-gtk3

« back to all changes in this revision

Viewing changes to tests/environ.py

  • Committer: Jaap Karssenberg
  • Date: 2014-03-08 11:47:43 UTC
  • mfrom: (668.1.49 pyzim-refactor)
  • Revision ID: jaap.karssenberg@gmail.com-20140308114743-fero6uvy9zirbb4o
Merge branch with refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Copyright 2013 Jaap Karssenberg <jaap.karssenberg@gmail.com>
 
4
 
 
5
from __future__ import with_statement
 
6
 
 
7
import tests
 
8
 
 
9
import os
 
10
 
 
11
from zim.environ import environ
 
12
from zim.fs import File, Dir
 
13
 
 
14
 
 
15
class EnvironmentContext(object):
 
16
        '''Context manager to be able to run test cases for
 
17
        environment parameters and restore the previous values on
 
18
        exit or error. Usage as::
 
19
 
 
20
                with EnvironmentContext({
 
21
                        'HOME': '/test/foo',
 
22
                        'XDG_HOME': None,
 
23
                }):
 
24
                        ...
 
25
        '''
 
26
        # Use os.environ here for to avoid any errors with
 
27
        # our environ object - may bite when testing with non-ascii data.
 
28
        # But overloaded in EnvironmentConfigContext()
 
29
 
 
30
        environ = os.environ
 
31
 
 
32
        def __init__(self, environ_context):
 
33
                self.environ_context = environ_context
 
34
                self.environ_backup = {}
 
35
 
 
36
        def __enter__(self):
 
37
                for k, v in self.environ_context.items():
 
38
                        self.environ_backup[k] = self.environ.get(k)
 
39
                        if v:
 
40
                                self.environ[k] = v
 
41
                        elif k in self.environ:
 
42
                                del self.environ[k]
 
43
                        else:
 
44
                                pass
 
45
 
 
46
        def __exit__(self, *exc_info):
 
47
                for k, v in self.environ_backup.items():
 
48
                        if v:
 
49
                                self.environ[k] = v
 
50
                        elif k in self.environ:
 
51
                                del self.environ[k]
 
52
                        else:
 
53
                                pass
 
54
 
 
55
                return False # Raise
 
56
 
 
57
 
 
58
 
 
59
class TestEnviron(tests.TestCase):
 
60
 
 
61
        def testGetSet(self):
 
62
                k = 'TEST_ZIM_ENVIRON_MODULE'
 
63
 
 
64
                with EnvironmentContext({k: None}):
 
65
                        self.assertNotIn(k, environ)
 
66
                        self.assertRaises(KeyError, environ.__getitem__, k)
 
67
                        self.assertIsNone(environ.get(k))
 
68
                        self.assertEqual(environ.get(k, 'FOO'), 'FOO')
 
69
 
 
70
                        environ[k] = 'BAR'
 
71
                        self.assertEqual(environ.get(k, 'FOO'), 'BAR')
 
72
                        self.assertEqual(environ[k], 'BAR')
 
73
                        self.assertGreater(len(environ), 0)
 
74
                        self.assertIn(k, environ)
 
75
 
 
76
                        del environ[k]
 
77
                        self.assertNotIn(k, environ)
 
78
 
 
79
        def testGetListPath(self):
 
80
                path = environ.get_list('PATH')
 
81
                self.assertGreater(len(path), 0)
 
82
                for dir in map(Dir, path):
 
83
                        if dir.exists():
 
84
                                break
 
85
                else:
 
86
                        raise AssertionError, 'No existing dirs found in PATH: %s' % path
 
87
 
 
88
        def testHomeAndUser(self):
 
89
                user = environ.get('USER')
 
90
                self.assertIsNotNone(user)
 
91
 
 
92
                home = environ.get('HOME')
 
93
                self.assertIsNotNone(home)
 
94
                self.assertTrue(Dir(home).exists())
 
95
 
 
96
                if os.name == 'nt':
 
97
                        appdata = environ.get('APPDATA')
 
98
                        self.assertIsNotNone(appdata)
 
99
                        self.assertTrue(Dir(appdata).exists())
 
100
 
 
101
 
 
102