Subject: [PATCH] package: don’t hard code port number in test

From: Michal Nazarewicz <[email-masked]>
Date: Mon, 6 Jun 2016 21:53:33 +0200

Hard coding port 8000 in package-tests.el causes the test to fail if
something is already listening on that port.  Change code so it uses
any available port.

* tests/lisp/emacs-lisp/package-tests.el
  (package-test-update-archives-async): Do not hard code port number
  and instead parse it from server’s standard output.

* tests/lisp/emacs-lisp/package-resources/package-test-server.py:
  Do not hard code port number.  Use any available one instead.
---
 .../package-resources/package-test-server.py       | 15 ++----
 test/lisp/emacs-lisp/package-tests.el              | 54 ++++++++++++----------
 2 files changed, 34 insertions(+), 35 deletions(-)

diff --git a/test/lisp/emacs-lisp/package-resources/package-test-server.py b/test/lisp/emacs-lisp/package-resources/package-test-server.py
index 35ca820..78e40b3 100644
--- a/test/lisp/emacs-lisp/package-resources/package-test-server.py
+++ b/test/lisp/emacs-lisp/package-resources/package-test-server.py
@@ -1,21 +1,16 @@
-import sys
+import os
 import BaseHTTPServer
 from SimpleHTTPServer import SimpleHTTPRequestHandler
 
-
 HandlerClass = SimpleHTTPRequestHandler
 ServerClass  = BaseHTTPServer.HTTPServer
 Protocol     = "HTTP/1.0"
 
-if sys.argv[1:]:
-    port = int(sys.argv[1])
-else:
-    port = 8000
-    server_address = ('127.0.0.1', port)
+os.chdir(os.path.dirname(__file__))
 
 HandlerClass.protocol_version = Protocol
-httpd = ServerClass(server_address, HandlerClass)
+httpd = ServerClass(('127.0.0.1', 0), HandlerClass)
 
-sa = httpd.socket.getsockname()
-print "Serving HTTP on", sa[0], "port", sa[1], "..."
+# This printed line is parsed by test code, don't change its format.
+print "Serving on http://%s:%s/" % httpd.socket.getsockname()
 httpd.serve_forever()
diff --git a/test/lisp/emacs-lisp/package-tests.el b/test/lisp/emacs-lisp/package-tests.el
index c7a5cc7..4ffb173 100644
--- a/test/lisp/emacs-lisp/package-tests.el
+++ b/test/lisp/emacs-lisp/package-tests.el
@@ -372,31 +372,35 @@ package-test-desc-version-string
   (skip-unless (executable-find "python2"))
   ;; For some reason this test doesn't work reliably on hydra.nixos.org.
   (skip-unless (not (getenv "NIX_STORE")))
-  (with-package-test (:basedir
-                      package-test-data-dir
-                      :location "http://0.0.0.0:8000/")
-    (let* ((package-menu-async t)
-           (process (start-process
-                     "package-server" "package-server-buffer"
-                     (executable-find "python2")
-                     (expand-file-name "package-test-server.py"))))
-      (unwind-protect
-          (progn
-            (list-packages)
-            (should package--downloads-in-progress)
-            (should mode-line-process)
-            (should-not
-             (with-timeout (10 'timeout)
-               (while package--downloads-in-progress
-                 (accept-process-output nil 1))
-               nil))
-            ;; If the server process died, there's some non-Emacs problem.
-            ;; Eg maybe the port was already in use.
-            (skip-unless (process-live-p process))
-            (goto-char (point-min))
-            (should
-             (search-forward-regexp "^ +simple-single" nil t)))
-        (if (process-live-p process) (kill-process process))))))
+
+  (with-temp-buffer
+    (cd package-test-data-dir)
+    (let ((package-menu-async t)
+          (process (start-process "package-server" (current-buffer)
+                                  (executable-find "python2")
+                                  (expand-file-name "package-test-server.py"))))
+      ;; Killing temp buffer will kill the process.
+      (set-process-query-on-exit-flag process nil)
+
+      (with-package-test
+          (:location (with-timeout (5 (should-not 'timeout))
+                       (while (and (goto-char (point-min))
+                                   (not (re-search-forward
+                                         "^Serving on \\(http://.*/\\)" nil t)))
+                         (accept-process-output process 1))
+                       (match-string 1)))
+        (list-packages)
+        (should package--downloads-in-progress)
+        (should mode-line-process)
+        (should-not (with-timeout (10 'timeout)
+                      (while package--downloads-in-progress
+                        (accept-process-output nil 1))
+                      nil))
+        ;; If the server process died, there's some non-Emacs problem.
+        ;; Eg maybe the port was already in use.
+        (skip-unless (process-live-p process))
+        (goto-char (point-min))
+        (should (search-forward-regexp "^ +simple-single" nil t))))))
 
 (ert-deftest package-test-describe-package ()
   "Test displaying help for a package."
-- 
2.8.0.rc3.226.g39d4020