You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

73 lines
2.1 KiB

  1. import nilmdb
  2. from nilmdb.printf import *
  3. import nilmdb.cmdline
  4. from nose.tools import *
  5. from nose.tools import assert_raises
  6. import json
  7. import itertools
  8. import os
  9. import shutil
  10. import sys
  11. import threading
  12. import urllib2
  13. from urllib2 import urlopen, HTTPError
  14. import Queue
  15. import StringIO
  16. import shlex
  17. from test_helpers import *
  18. testdb = "tests/cmdline-testdb"
  19. class TestCmdline(object):
  20. def setUp(self):
  21. # Clear out DB
  22. recursive_unlink(testdb)
  23. # Start web app on a custom port
  24. self.db = nilmdb.NilmDB(testdb, sync=False)
  25. self.server = nilmdb.Server(self.db, host = "127.0.0.1",
  26. port = 12380, stoppable = False)
  27. self.server.start(blocking = False)
  28. def tearDown(self):
  29. # Close web app
  30. self.server.stop()
  31. self.db.close()
  32. def run(self, arg_string, input_string = None):
  33. """Run a cmdline client with the specified argument string,
  34. passing the given input. Returns a tuple with the output and
  35. exit code"""
  36. class stdio_wrapper:
  37. def __init__(self, stdin, stdout, stderr):
  38. self.io = (stdin, stdout, stderr)
  39. def __enter__(self):
  40. self.saved = ( sys.stdin, sys.stdout, sys.stderr )
  41. ( sys.stdin, sys.stdout, sys.stderr ) = self.io
  42. def __exit__(self, type, value, traceback):
  43. ( sys.stdin, sys.stdout, sys.stderr ) = self.saved
  44. infile = StringIO.StringIO(input_string)
  45. outfile = StringIO.StringIO()
  46. with stdio_wrapper(infile, outfile, outfile) as s:
  47. try:
  48. nilmdb.cmdline.run(args = shlex.split(arg_string))
  49. sys.exit(0)
  50. except SystemExit as e:
  51. exitcode = e.code
  52. output = outfile.getvalue()
  53. return (output, exitcode)
  54. def test_cmdline_basic(self):
  55. (output, exitcode) = self.run("--help")
  56. assert("Usage:" in output)
  57. eq_(exitcode, 0)
  58. (output, exitcode) = self.run("--nosuchoption")
  59. ne_(exitcode, 0)
  60. (output, exitcode) = self.run("--url http://localhost:12380/")
  61. eq_(exitcode, 0)