refactor existing tests for click cli
MODIFY
tests/conftest.py
tests/conftest.py
@@ -140,13 +140,11 @@ def netcdf_http_paths(netcdf_local_paths, request):
command_list = [
"python",
os.path.join(this_dir, "http_auth_server.py"),
- port,
- "127.0.0.1",
- username,
- password,
+ f"--port={port}",
+ "--address=127.0.0.1",
]
if username:
- command_list += [username, password]
+ command_list += [f"--username={username}", f"--password={password}"]
p = subprocess.Popen(command_list, cwd=basedir)
url = f"http://127.0.0.1:{port}"
time.sleep(2) # let the server start up

MODIFY
tests/http_auth_server.py
tests/http_auth_server.py
import base64
import http.server
import socketserver
-import sys
-port_str, ADDRESS = sys.argv[1:3]
-PORT = int(port_str)
-if len(sys.argv) > 3:
- username, password = sys.argv[3:5]
-else:
- username, password = "", ""
+import click
+@click.command()
+@click.option("--address")
+@click.option("--port")
+@click.option("--username")
+@click.option("--password")
+def serve_forever(address, port, username, password):
+
+ port = int(port)
+
class Handler(http.server.SimpleHTTPRequestHandler):
+
def do_GET(self):
if username:
auth = self.headers.get("Authorization")
@@ -27,7 +31,10 @@ class Handler(http.server.SimpleHTTPRequestHandler):
return
return http.server.SimpleHTTPRequestHandler.do_GET(self)
-
socketserver.TCPServer.allow_reuse_address = True
-with socketserver.TCPServer((ADDRESS, PORT), Handler) as httpd:
+ with socketserver.TCPServer((address, port), Handler) as httpd:
httpd.serve_forever()
+
+
+if __name__ == '__main__':
+ serve_forever()

refactor existing tests for click cli