forked from OfflineIMAP/offlineimap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create global instance of command-line options
This eases testing of option values inside the code. This instance is implemented as the read-only copy of the obtained 'options' object, so callers won't be able to modify its contents. Signed-off-by: Eygene Ryabinkin <[email protected]>
- Loading branch information
Showing
7 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright 2013 Eygene A. Ryabinkin. | ||
# | ||
# Module that holds various global objects. | ||
|
||
from offlineimap.utils import const | ||
|
||
# Holds command-line options for OfflineIMAP. | ||
options = const.ConstProxy() | ||
|
||
def set_options (source): | ||
""" Sets the source for options variable """ | ||
options.set_source (source) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2013 Eygene A. Ryabinkin. | ||
# | ||
# Collection of classes that implement const-like behaviour | ||
# for various objects. | ||
|
||
import copy | ||
|
||
class ConstProxy (object): | ||
""" | ||
Implements read-only access to a given object | ||
that can be attached to each instance only once. | ||
""" | ||
|
||
def __init__ (self): | ||
self.__dict__['__source'] = None | ||
|
||
|
||
def __getattr__ (self, name): | ||
src = self.__dict__['__source'] | ||
if src == None: | ||
raise ValueError ("using non-initialized ConstProxy() object") | ||
return copy.deepcopy (getattr (src, name)) | ||
|
||
|
||
def __setattr__ (self, name, value): | ||
raise AttributeError ("tried to set '%s' to '%s' for constant object" % \ | ||
(name, value)) | ||
|
||
|
||
def __delattr__ (self, name): | ||
raise RuntimeError ("tried to delete field '%s' from constant object" % \ | ||
(name)) | ||
|
||
|
||
def set_source (self, source): | ||
""" Sets source object for this instance. """ | ||
if (self.__dict__['__source'] != None): | ||
raise ValueError ("source object is already set") | ||
self.__dict__['__source'] = source |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python | ||
# Copyright 2013 Eygene A. Ryabinkin | ||
|
||
from offlineimap import globals | ||
import unittest | ||
|
||
class Opt: | ||
def __init__(self): | ||
self.one = "baz" | ||
self.two = 42 | ||
self.three = True | ||
|
||
|
||
class TestOfflineimapGlobals(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(klass): | ||
klass.o = Opt() | ||
globals.set_options (klass.o) | ||
|
||
def test_initial_state(self): | ||
for k in self.o.__dict__.keys(): | ||
self.assertTrue(getattr(self.o, k) == | ||
getattr(globals.options, k)) | ||
|
||
def test_object_changes(self): | ||
self.o.one = "one" | ||
self.o.two = 119 | ||
self.o.three = False | ||
return self.test_initial_state() | ||
|
||
def test_modification(self): | ||
with self.assertRaises(AttributeError): | ||
globals.options.two = True | ||
|
||
def test_deletion(self): | ||
with self.assertRaises(RuntimeError): | ||
del globals.options.three | ||
|
||
def test_nonexistent_key(self): | ||
with self.assertRaises(AttributeError): | ||
a = globals.options.nosuchoption | ||
|
||
def test_double_init(self): | ||
with self.assertRaises(ValueError): | ||
globals.set_options (True) | ||
|
||
|
||
if __name__ == "__main__": | ||
suite = unittest.TestLoader().loadTestsFromTestCase(TestOfflineimapGlobals) | ||
unittest.TextTestRunner(verbosity=2).run(suite) |