Skip to content

Commit

Permalink
Refactor test imports to use path management helpers
Browse files Browse the repository at this point in the history
Replaces a relative import (which prevented tests scripts from simply
being runnable) with a single line of boilerplate to set up the path and
an import that further alters the path as required.
  • Loading branch information
c-mita committed Jun 23, 2016
1 parent 7cc03e4 commit 7d78d16
Show file tree
Hide file tree
Showing 30 changed files with 161 additions and 65 deletions.
5 changes: 5 additions & 0 deletions malcolm/wscomms/wsservercomms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict
import json

Expand Down
7 changes: 7 additions & 0 deletions tests/setup_malcolm_paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "scanpointgenerator"))

from pkg_resources import require
require("mock", "numpy", "tornado")
8 changes: 6 additions & 2 deletions tests/test_controllers/test_clientcontroller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict

from . import util
import unittest
from mock import MagicMock, patch

# logging
Expand Down
7 changes: 5 additions & 2 deletions tests/test_controllers/test_countercontroller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from . import util
import unittest
from mock import Mock

from malcolm.controllers.countercontroller import CounterController
Expand Down
7 changes: 5 additions & 2 deletions tests/test_controllers/test_hellocontroller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from . import util
import unittest
from mock import Mock

from malcolm.controllers.hellocontroller import HelloController
Expand Down
1 change: 0 additions & 1 deletion tests/test_controllers/util.py

This file was deleted.

8 changes: 6 additions & 2 deletions tests/test_core/test_attribute.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict

from . import util
import unittest
from mock import Mock, patch

from malcolm.core.attribute import Attribute
Expand Down
9 changes: 7 additions & 2 deletions tests/test_core/test_attributemeta.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from . import util
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict

import unittest
from mock import MagicMock

from malcolm.core.attributemeta import AttributeMeta
Expand Down
8 changes: 6 additions & 2 deletions tests/test_core/test_block.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict

from . import util
import unittest
from mock import MagicMock, patch

# module imports
Expand Down
9 changes: 8 additions & 1 deletion tests/test_core/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import os
import sys
import unittest
from collections import OrderedDict
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))

# import logging
# logging.basicConfig(level=logging.DEBUG)

from . import util
import setup_malcolm_paths
from mock import MagicMock

# module imports
Expand Down Expand Up @@ -38,3 +42,6 @@ def test_walkt_path(self):
c[1] = {2: {3: "end"}}
walked = c.walk_path([1, 2, 3])
self.assertEqual(walked, "end")

if __name__ == "__main__":
unittest.main()
9 changes: 7 additions & 2 deletions tests/test_core/test_clientcomms.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict

from . import util
import unittest
from mock import Mock, patch, call

from malcolm.core.clientcomms import ClientComms
from malcolm.core.syncfactory import SyncFactory


class TestClientComms(unittest.TestCase):
@patch("malcolm.core.clientcomms.ClientComms.add_spawn_function")
@patch("malcolm.core.clientcomms.ClientComms.make_default_stop_func")
Expand Down
7 changes: 5 additions & 2 deletions tests/test_core/test_controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from . import util
import unittest
from mock import MagicMock, call

# module imports
Expand Down
7 changes: 5 additions & 2 deletions tests/test_core/test_loggable.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from . import util
import unittest
from mock import patch

from malcolm.core.loggable import Loggable
Expand Down
8 changes: 6 additions & 2 deletions tests/test_core/test_mapmeta.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict

from . import util
import unittest
from mock import MagicMock, patch, call

from malcolm.core.mapmeta import MapMeta
Expand Down
8 changes: 6 additions & 2 deletions tests/test_core/test_method.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict

from . import util
import unittest
from mock import Mock, patch, call, MagicMock

from malcolm.core.method import Method, takes, returns
Expand Down
8 changes: 6 additions & 2 deletions tests/test_core/test_monitorable.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from . import util
import unittest
from mock import Mock

from malcolm.core.monitorable import Monitorable


class TestMonitorable(unittest.TestCase):

def test_init(self):
Expand Down
11 changes: 8 additions & 3 deletions tests/test_core/test_numbermeta.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from . import util
import unittest
from collections import OrderedDict
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict
import numpy as np

import unittest

from malcolm.core.numbermeta import NumberMeta
from malcolm.core.attributemeta import AttributeMeta


class TestNumberMeta(unittest.TestCase):
def test_init_int(self):
nm = NumberMeta("nm", "desc", np.int32)
Expand Down
8 changes: 6 additions & 2 deletions tests/test_core/test_process.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict
# import logging
# logging.basicConfig(level=logging.DEBUG)

from . import util
import unittest
from mock import MagicMock

# module imports
Expand Down
8 changes: 6 additions & 2 deletions tests/test_core/test_request.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict

from . import util
import unittest
from mock import MagicMock, patch

from malcolm.core.request import Request
Expand Down
8 changes: 6 additions & 2 deletions tests/test_core/test_response.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict

from . import util
import unittest
from mock import Mock, MagicMock

from malcolm.core.response import Response
Expand Down
8 changes: 6 additions & 2 deletions tests/test_core/test_servercomms.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from . import util
import unittest
from mock import Mock, patch, call

from malcolm.core.servercomms import ServerComms
from malcolm.core.spawnable import Spawnable
from malcolm.core.syncfactory import SyncFactory


class TestServerComms(unittest.TestCase):

def setUp(self):
Expand Down
5 changes: 4 additions & 1 deletion tests/test_core/test_spawnable.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
import sys
import unittest
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))

from . import util
import setup_malcolm_paths
from mock import Mock, call

from malcolm.core.spawnable import Spawnable
Expand Down
8 changes: 6 additions & 2 deletions tests/test_core/test_stringmeta.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict

from . import util
import unittest

from malcolm.core.stringmeta import StringMeta
from malcolm.core.attributemeta import AttributeMeta
Expand Down
7 changes: 5 additions & 2 deletions tests/test_core/test_syncfactory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from . import util
import unittest
from mock import patch

# module imports
Expand Down
7 changes: 5 additions & 2 deletions tests/test_core/test_system_core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from . import util
import unittest

# logging
# import logging
Expand Down
6 changes: 0 additions & 6 deletions tests/test_core/util.py

This file was deleted.

13 changes: 7 additions & 6 deletions tests/test_wscomms/test_system_wscomms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import unittest
import time

from . import util
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

import time
# logging
# import logging
# logging.basicConfig(level=logging.DEBUG)

import unittest

# tornado
from pkg_resources import require
require("tornado")
from tornado.websocket import websocket_connect
from tornado import gen
from tornado.ioloop import IOLoop
Expand Down
10 changes: 6 additions & 4 deletions tests/test_wscomms/test_wsclientcomms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from . import util
import unittest
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict

from pkg_resources import require
require('tornado')
import unittest
from mock import MagicMock, patch, call

from malcolm.wscomms.wsclientcomms import WSClientComms
Expand Down

0 comments on commit 7d78d16

Please sign in to comment.