diff --git a/MANIFEST.in b/MANIFEST.in index 2f3629b..fe386c5 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -10,3 +10,4 @@ include pypiserver/bottle.py include pypiserver/core.py include setup.cfg include setup.py +include tests/test_main.py diff --git a/pypiserver/core.py b/pypiserver/core.py index 9fc633a..d92073d 100755 --- a/pypiserver/core.py +++ b/pypiserver/core.py @@ -183,7 +183,10 @@ Visit http://pypi.python.org/pypi/pypiserver for more information. """ -def main(): +def main(argv=None): + if argv is None: + argv = sys.argv + global packages host = "0.0.0.0" @@ -191,7 +194,7 @@ def main(): server = None try: - opts, roots = getopt.getopt(sys.argv[1:], "i:p:r:h", ["interface=", "port=", "root=", "server=", "disable-fallback", "version", "help"]) + opts, roots = getopt.getopt(argv[1:], "i:p:r:h", ["interface=", "port=", "root=", "server=", "disable-fallback", "version", "help"]) except getopt.GetoptError, err: sys.exit("usage error: %s" % (err,)) @@ -236,4 +239,4 @@ def main(): if __name__ == "__main__": - main() + main(sys.argv) diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100755 index 0000000..da0d35b --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,76 @@ +#! /usr/bin/env py.test + +import os +import pytest +from pypiserver import core + + +class main_wrapper(object): + + def __init__(self): + self.run_kwargs = None + self.pkgdir = None + + def __call__(self, argv): + print "Running", argv + core.main(["pypi-server"] + argv) + return self.run_kwargs + + +def pytest_funcarg__main(request): + + main = main_wrapper() + + def run(**kwargs): + print "RUN:", kwargs + main.run_kwargs = kwargs + + def listdir(pkgdir): + main.pkgdir = pkgdir + return [] + + monkeypatch = request.getfuncargvalue("monkeypatch") + monkeypatch.setattr(core, "run", run) + monkeypatch.setattr(os, "listdir", listdir) + monkeypatch.setattr(core, "packages", None) + monkeypatch.setattr(core, "config", core.configuration()) + + return main + + +def test_default_pkgdir(main): + main([]) + assert main.pkgdir == os.path.expanduser("~/packages") + + +def test_noargs(main): + assert main([]) == dict(host="0.0.0.0", port=8080, server="auto") + + +def test_port(main): + expected = dict(host="0.0.0.0", port=8081, server="auto") + assert main(["--port=8081"]) == expected + assert main(["--port", "8081"]) == expected + assert main(["-p", "8081"]) == expected + + +def test_server(main): + assert main(["--server=paste"])["server"] == "paste" + assert main(["--server", "cherrypy"])["server"] == "cherrypy" + + +def test_root(main): + main(["--root", "."]) + assert core.packages.root == os.path.abspath(".") + assert main.pkgdir == os.path.abspath(".") + + +def test_root_r(main): + main(["-r", "."]) + assert core.packages.root == os.path.abspath(".") + assert main.pkgdir == os.path.abspath(".") + + +def test_root_multiple(main): + pytest.raises(SystemExit, main, [".", "."]) + pytest.raises(SystemExit, main, ["-r", ".", "."])