From 33acbaeaf17bc5c38d0889affd8c5be5363f2704 Mon Sep 17 00:00:00 2001
From: Matthew Flatt <mflatt@racket-lang.org>
Date: Thu, 10 Mar 2016 16:54:28 -0700
Subject: [PATCH] fix tests to avoid writing to the current or installation
 directory

---
 pkgs/racket-test-core/tests/racket/file.rktl  |   7 ++
 .../tests/racket/filelib.rktl                 | 116 +++++++++++-------
 .../tests/racket/foreign-test.rktl            |  57 +++++----
 pkgs/racket-test-core/tests/racket/path.rktl  |  13 +-
 pkgs/racket-test-core/tests/racket/port.rktl  |  42 ++++---
 pkgs/racket-test/tests/pkg/tests-install.rkt  |  58 ++++-----
 pkgs/racket-test/tests/pkg/tests-promote.rkt  |  12 +-
 pkgs/racket-test/tests/pkg/tests-remove.rkt   |   8 +-
 pkgs/racket-test/tests/pkg/util.rkt           |  19 ++-
 pkgs/racket-test/tests/racket/old-make-zo.rkt |  15 ++-
 10 files changed, 216 insertions(+), 131 deletions(-)

diff --git a/pkgs/racket-test-core/tests/racket/file.rktl b/pkgs/racket-test-core/tests/racket/file.rktl
index 23a44ba285..1f9e78d6a7 100644
--- a/pkgs/racket-test-core/tests/racket/file.rktl
+++ b/pkgs/racket-test-core/tests/racket/file.rktl
@@ -5,6 +5,10 @@
 
 (define testing.rktl (build-path (current-load-relative-directory) "testing.rktl"))
 
+(define original-dir (current-directory))
+(define work-dir (make-temporary-file "path~a" 'directory))
+(current-directory work-dir)
+
 (test #t input-port? (current-input-port))
 (test #t output-port? (current-output-port))
 (test #t output-port? (current-error-port))
@@ -1615,6 +1619,9 @@
 ;; Cleanup files created above
 (for ([f '("tmp1" "tmp2" "tmp3")] #:when (file-exists? f)) (delete-file f))
 
+(current-directory original-dir)
+(delete-directory work-dir)
+
 ;; Network - - - - - - - - - - - - - - - - - - - - - -
 
 (define (net-reject? who host port what)
diff --git a/pkgs/racket-test-core/tests/racket/filelib.rktl b/pkgs/racket-test-core/tests/racket/filelib.rktl
index a76c8b8754..b1075f914d 100644
--- a/pkgs/racket-test-core/tests/racket/filelib.rktl
+++ b/pkgs/racket-test-core/tests/racket/filelib.rktl
@@ -7,7 +7,8 @@
 	 racket/system
 	 racket/list)
 
-(define tmp-name "tmp0-filelib")
+(define tmp-dir (make-temporary-file "filelib~a" 'directory))
+(define tmp-name (build-path tmp-dir "tmp0-filelib"))
 (when (file-exists? tmp-name) (delete-file tmp-name))
 (display-lines-to-file '("a" "b" "c") tmp-name #:separator #"\r\n" #:mode 'binary)
 (test '(a b c) file->list tmp-name)
@@ -28,6 +29,7 @@
 (test #"\"\316\273\"" file->bytes tmp-name)
 (test "\u03BB" file->value tmp-name)
 (when (file-exists? tmp-name) (delete-file tmp-name))
+(delete-directory tmp-dir)
 
 (define-syntax-rule (err/rt-chk-test (op arg ...))
   (err/rt-test (op arg ...) (check-msg 'op)))
@@ -87,57 +89,79 @@
       (test #t equal? (sort rel) (sort rel2))
 
       (unless (eq? (system-type) 'windows)
-        (make-file-or-directory-link "filelib.rktl" "filelib-link")
-        (make-file-or-directory-link "." "loop-link")
+        (define tmp-dir (make-temporary-file "filelib~a" 'directory))
+        (define (touch . elems)
+          (call-with-output-file
+           (apply build-path elems)
+           void))
+        
+        (copy-file "filelib.rktl" (build-path tmp-dir "filelib.rktl"))
+        (make-directory (build-path tmp-dir "sub"))
+        (touch tmp-dir "a")
+        (touch tmp-dir "b")
+        (touch tmp-dir "sub" "x")
+         
+        (parameterize ([current-directory tmp-dir])
+          (define rel2 (fold-files (lambda (name kind accum)
+                                     (test kind name (if (file-exists? name)
+                                                         'file
+                                                         'dir))
+                                     (cons name accum))
+                                   null))
+          
+          (make-file-or-directory-link "filelib.rktl" "filelib-link")
+          (make-file-or-directory-link "." "loop-link")
 
-	(test (+ 2 (length rel2))
-	      fold-files 
-	      (lambda (name kind accum)
-		(test kind values (cond
-				   [(link-exists? name) 'link]
-				   [(file-exists? name) 'file]
-				   [(directory-exists? name) 'dir]
-				   [else '???]))
-		(when (member name '("filelib-link" "loop-link"))
-		  (test kind name 'link))
-		(add1 accum))
-	      0
-	      #f
-	      #f)
+          (test (+ 2 (length rel2))
+                fold-files 
+                (lambda (name kind accum)
+                  (test kind values (cond
+                                     [(link-exists? name) 'link]
+                                     [(file-exists? name) 'file]
+                                     [(directory-exists? name) 'dir]
+                                     [else '???]))
+                  (when (member name '("filelib-link" "loop-link"))
+                    (test kind name 'link))
+                  (add1 accum))
+                0
+                #f
+                #f)
 
-	(test (+ 2 (length rel2))
-	      fold-files 
-	      (lambda (name kind accum)
-		(test kind values (cond
-				   [(link-exists? name) 'link]
-				   [(file-exists? name) 'file]
-				   [(directory-exists? name) 'dir]
-				   [else '???]))
-		(when (member name '("filelib-link" "loop-link"))
-		  (test kind name 'link))
-		(values (add1 accum) #t))
-	      0
-	      #f
-	      #f)
+          (test (+ 2 (length rel2))
+                fold-files 
+                (lambda (name kind accum)
+                  (test kind values (cond
+                                     [(link-exists? name) 'link]
+                                     [(file-exists? name) 'file]
+                                     [(directory-exists? name) 'dir]
+                                     [else '???]))
+                  (when (member name '("filelib-link" "loop-link"))
+                    (test kind name 'link))
+                  (values (add1 accum) #t))
+                0
+                #f
+                #f)
 
-        (delete-file "loop-link")
+          (delete-file "loop-link")
 
-	(test (+ 1 (length rel2))
-	      fold-files 
-	      (lambda (name kind accum)
-		(test kind values (cond
-				   [(file-exists? name) 'file]
-				   [else 'dir]))
-		(when (member name '("filelib-link"))
-		  (test kind name 'file))
-		(add1 accum))
-	      0
-	      #f
-	      #t)
+          (test (+ 1 (length rel2))
+                fold-files 
+                (lambda (name kind accum)
+                  (test kind values (cond
+                                     [(file-exists? name) 'file]
+                                     [else 'dir]))
+                  (when (member name '("filelib-link"))
+                    (test kind name 'file))
+                  (add1 accum))
+                0
+                #f
+                #t)
 
-	(delete-file "filelib-link")
+          (delete-file "filelib-link")
+
+          'done)
+        (delete-directory/files tmp-dir)))))
 
-	'done))))
 ;; ----------------------------------------
 
 ;;----------------------------------------------------------------------
diff --git a/pkgs/racket-test-core/tests/racket/foreign-test.rktl b/pkgs/racket-test-core/tests/racket/foreign-test.rktl
index 6ff8283dc4..cf620e5d72 100644
--- a/pkgs/racket-test-core/tests/racket/foreign-test.rktl
+++ b/pkgs/racket-test-core/tests/racket/foreign-test.rktl
@@ -7,7 +7,8 @@
          ffi/unsafe/cvector
          ffi/vector
          racket/extflonum
-         racket/place)
+         racket/place
+         racket/file)
 
 (define test-async? (and (place-enabled?) (not (eq? 'windows (system-type)))))
 
@@ -97,34 +98,38 @@
 
 (require dynext/compile dynext/link racket/runtime-path)
 (define-runtime-path here ".")
+
+(define test-tmp-dir
+  (make-temporary-file "foreign~a" 'directory))
+(copy-file (build-path here "foreign-test.c")
+           (build-path test-tmp-dir "foreign-test.c"))
 (define delete-test-files
-  (let ([c  (build-path here "foreign-test.c")]
-        [o  (build-path (current-directory)
-                        (if (eq? 'windows (system-type))
-                          "foreign-test.obj" "foreign-test.o"))]
-        [so (build-path (current-directory)
-                        (bytes->path (bytes-append #"foreign-test"
-                                                   (system-type 'so-suffix))))])
-    (when (file-exists? o) (delete-file o))
-    (when (file-exists? so) (delete-file so))
-    (parameterize ([current-standard-link-libraries '()]
-                   [current-extension-compiler-flags
-                    (if test-async?
-                        (append '("-pthread" "-DUSE_THREAD_TEST") (current-extension-compiler-flags))
-                        (current-extension-compiler-flags))]
-                   [current-extension-linker-flags
-                    (if test-async?
-                        (append '("-pthread") (current-extension-linker-flags))
-                        (current-extension-linker-flags))])
-      (compile-extension #t c o '())
-      (link-extension #t (list o) so))
-    (lambda ()
+  (parameterize ([current-directory test-tmp-dir])
+    (let ([c  (build-path (current-directory) "foreign-test.c")]
+          [o  (build-path (current-directory)
+                          (if (eq? 'windows (system-type))
+                              "foreign-test.obj" "foreign-test.o"))]
+          [so (build-path (current-directory)
+                          (bytes->path (bytes-append #"foreign-test"
+                                                     (system-type 'so-suffix))))])
       (when (file-exists? o) (delete-file o))
-      (when (file-exists? so)
+      (when (file-exists? so) (delete-file so))
+      (parameterize ([current-standard-link-libraries '()]
+                     [current-extension-compiler-flags
+                      (if test-async?
+                          (append '("-pthread" "-DUSE_THREAD_TEST") (current-extension-compiler-flags))
+                          (current-extension-compiler-flags))]
+                     [current-extension-linker-flags
+                      (if test-async?
+                          (append '("-pthread") (current-extension-linker-flags))
+                          (current-extension-linker-flags))])
+        (compile-extension #t c o '())
+        (link-extension #t (list o) so))
+      (lambda ()
         (with-handlers ([exn:fail:filesystem?
                          (lambda (e)
-                           (eprintf "warning: could not delete ~e\n" so))])
-          (delete-file so))))))
+                           (eprintf "warning: could not delete ~e\n" test-tmp-dir))])
+          (delete-directory/files test-tmp-dir))))))
 
 ;; Test arrays
 (define _c7_list (_array/list _byte 7))
@@ -179,7 +184,7 @@
 (define _borl (_union _byte _long))
 (define _ic7iorl (_union _ic7i _long))
 
-(define test-lib (ffi-lib "./foreign-test"))
+(define test-lib (ffi-lib (build-path test-tmp-dir "foreign-test")))
 
 (for ([n (in-range 5)])
   (define (ffi name type) (get-ffi-obj name test-lib type))
diff --git a/pkgs/racket-test-core/tests/racket/path.rktl b/pkgs/racket-test-core/tests/racket/path.rktl
index ff374f935b..c609eee4c1 100644
--- a/pkgs/racket-test-core/tests/racket/path.rktl
+++ b/pkgs/racket-test-core/tests/racket/path.rktl
@@ -71,6 +71,10 @@
 (arity-test complete-path? 1 1)
 (err/rt-test (complete-path? 'a))
 
+(define original-dir (current-directory))
+(define work-dir (make-temporary-file "path~a" 'directory))
+(current-directory work-dir)
+
 (call-with-output-file "tmp6" void #:exists 'replace)
 (define existant "tmp6")
 
@@ -220,6 +224,9 @@
 (test #t delete-directory/tf "down")
 (test #f delete-directory/tf "down")
 
+(current-directory original-dir)
+(delete-directory work-dir)
+
 ; Redefine these per-platform
 (define drives null)
 (define nondrive-roots (list "/"))
@@ -875,12 +882,12 @@
     (test (bytes->path #"/home/mflatt/././~") build-path (bytes->path #"/home/mflatt") (bytes->path #"././~"))
     (test (bytes->path #"./~") build-path (bytes->path #"./~"))
     (when use-fs?
-      (let ([dir "tmp79"])
+      (let ([dir (make-temporary-file "tmp79~a" 'directory)])
         (unless (directory-exists? dir)
           (make-directory dir))
-        (close-output-port (open-output-file "tmp79/~me" #:exists 'replace))
+        (close-output-port (open-output-file (build-path dir "~me") #:exists 'replace))
         (test (list (bytes->path #"~me")) directory-list dir)
-        (delete-file (build-path "tmp79" (bytes->path #"~me")))
+        (delete-file (build-path dir (bytes->path #"~me")))
         (delete-directory dir)))
     (void)))
 
diff --git a/pkgs/racket-test-core/tests/racket/port.rktl b/pkgs/racket-test-core/tests/racket/port.rktl
index d53324ace8..ada754c2a3 100644
--- a/pkgs/racket-test-core/tests/racket/port.rktl
+++ b/pkgs/racket-test-core/tests/racket/port.rktl
@@ -3,6 +3,15 @@
 
 (Section 'port)
 
+(define (call-in-temporary-directory thunk)
+  (define dir (make-temporary-file "tmp~a" 'directory))
+  (dynamic-wind
+   void
+   (lambda ()
+     (parameterize ([current-directory dir])
+       (thunk)))
+   (lambda () (delete-directory dir))))
+
 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Tests for progress events and commits
 
@@ -57,12 +66,15 @@
   (test-pipe #t))
 (let ([test-file
        (lambda (commit-eof?)
-         (with-output-to-file "tmp8" #:exists 'truncate/replace
-           (lambda () (write-string "hello")))
-         (define p (open-input-file "tmp8"))
-         (test-hello-port p commit-eof?)
-         (close-input-port p)
-         (delete-file "tmp8"))])
+         (call-in-temporary-directory
+          (lambda ()
+            (with-output-to-file "tmp8"
+              #:exists 'truncate/replace
+              (lambda () (write-string "hello")))
+            (define p (open-input-file "tmp8"))
+            (test-hello-port p commit-eof?)
+            (close-input-port p)
+            (delete-file "tmp8"))))])
   (test-file #f)
   (test-file #t))
 
@@ -777,14 +789,16 @@
       (count-lines! in)
       (check in))
     (let ()
-      (with-output-to-file "tmp8" 
-        #:exists 'truncate/replace
-        (lambda () (display "12345")))
-      (define in (open-input-file "tmp8"))
-      (count-lines! in)
-      (check in)
-      (close-input-port in)
-      (delete-file "tmp8")))
+      (call-in-temporary-directory
+       (lambda ()
+         (with-output-to-file "tmp8" 
+           #:exists 'truncate/replace
+           (lambda () (display "12345")))
+         (define in (open-input-file "tmp8"))
+         (count-lines! in)
+         (check in)
+         (close-input-port in)
+         (delete-file "tmp8")))))
   (check-all void)
   (check-all port-count-lines!))
 
diff --git a/pkgs/racket-test/tests/pkg/tests-install.rkt b/pkgs/racket-test/tests/pkg/tests-install.rkt
index 78b6bdc4e0..68f70a048d 100644
--- a/pkgs/racket-test/tests/pkg/tests-install.rkt
+++ b/pkgs/racket-test/tests/pkg/tests-install.rkt
@@ -119,17 +119,18 @@
     "local directory fails when not there"
     $ "raco pkg install --copy test-pkgs/pkg-test1-not-there/" =exit> 1)
 
-   (shelly-case
-    "directory fails due to path overlap"
-    $ "raco pkg install test-pkgs/pkg-test1"
-    =exit> 1
-    =stderr> #rx"overlap"
-    $ (~a "raco pkg install " (find-collects-dir))
-    =exit> 1
-    =stderr> #rx"overlap.*collection"
-    $ (~a "raco pkg install " (collection-path "tests"))
-    =exit> 1
-    =stderr> #rx"overlap.*package")
+   (parameterize ([current-directory test-source-directory])
+     (shelly-case
+      "directory fails due to path overlap"
+      $ "raco pkg install test-pkgs/pkg-test1"
+      =exit> 1
+      =stderr> #rx"overlap"
+      $ (~a "raco pkg install " (find-collects-dir))
+      =exit> 1
+      =stderr> #rx"overlap.*collection"
+      $ (~a "raco pkg install " (collection-path "tests"))
+      =exit> 1
+      =stderr> #rx"overlap.*package"))
 
    (define tmp-dir (path->directory-path (make-temporary-file "pkg~a" 'directory)))
    $ (~a "cp -r test-pkgs/pkg-test1 "tmp-dir"pkg-test1")
@@ -165,23 +166,24 @@
    $ (~a "cp -r "tmp-dir"pkg-test1 "tmp-dir"pkg-test1-linking")
    $ (~a "cp -r test-pkgs/pkg-test1-staging "tmp-dir"pkg-test1-staging")
 
-   (with-fake-root
-    (shelly-case
-     "linking local directory"
-     $ "racket -e '(require pkg-test1)'" =exit> 1
-     $ (~a "raco pkg install --link "tmp-dir"pkg-test1-linking")
-     $ "racket -e '(require pkg-test1)'"
-     $ "racket -e '(require pkg-test1/a)'" =exit> 1
-     $ (~a "racket -e '(require pkg/lib)' -e '(path->pkg \""tmp-dir"pkg-test1-linking\")'") =stdout> "\"pkg-test1-linking\"\n"
-     $ (~a "racket -e '(require pkg/lib)' -e '(path->pkg \""tmp-dir"pkg-test1-linking/README\")'") =stdout> "\"pkg-test1-linking\"\n"
-     $ "racket -e '(require pkg/lib)' -e '(path->pkg \"test-pkgs\")'" =stdout> "\"racket-test\"\n"
-     $ "racket -e '(require pkg/lib)' -e '(path->pkg (collection-file-path \"main.rkt\" \"racket\"))'" =stdout> "#f\n"
-     $ (~a "cp "tmp-dir"pkg-test1-staging/a.rkt "tmp-dir"pkg-test1-linking/pkg-test1/a.rkt")
-     $ "racket -e '(require pkg-test1/a)'"
-     $ (~a "rm -f "tmp-dir"pkg-test1-linking/pkg-test1/a.rkt")
-     $ "racket -e '(require pkg-test1/a)'" =exit> 1
-     $ "raco pkg remove pkg-test1-linking"
-     $ "racket -e '(require pkg-test1)'" =exit> 1))
+   (parameterize ([current-directory test-source-directory])
+     (with-fake-root
+      (shelly-case
+       "linking local directory"
+       $ "racket -e '(require pkg-test1)'" =exit> 1
+       $ (~a "raco pkg install --link "tmp-dir"pkg-test1-linking")
+       $ "racket -e '(require pkg-test1)'"
+       $ "racket -e '(require pkg-test1/a)'" =exit> 1
+       $ (~a "racket -e '(require pkg/lib)' -e '(path->pkg \""tmp-dir"pkg-test1-linking\")'") =stdout> "\"pkg-test1-linking\"\n"
+       $ (~a "racket -e '(require pkg/lib)' -e '(path->pkg \""tmp-dir"pkg-test1-linking/README\")'") =stdout> "\"pkg-test1-linking\"\n"
+       $ "racket -e '(require pkg/lib)' -e '(path->pkg \"test-pkgs\")'" =stdout> "\"racket-test\"\n"
+       $ "racket -e '(require pkg/lib)' -e '(path->pkg (collection-file-path \"main.rkt\" \"racket\"))'" =stdout> "#f\n"
+       $ (~a "cp "tmp-dir"pkg-test1-staging/a.rkt "tmp-dir"pkg-test1-linking/pkg-test1/a.rkt")
+       $ "racket -e '(require pkg-test1/a)'"
+       $ (~a "rm -f "tmp-dir"pkg-test1-linking/pkg-test1/a.rkt")
+       $ "racket -e '(require pkg-test1/a)'" =exit> 1
+       $ "raco pkg remove pkg-test1-linking"
+       $ "racket -e '(require pkg-test1)'" =exit> 1)))
 
    $ (~a "cp -r "tmp-dir"pkg-test3 "tmp-dir"pkg-test3-linking")
 
diff --git a/pkgs/racket-test/tests/pkg/tests-promote.rkt b/pkgs/racket-test/tests/pkg/tests-promote.rkt
index ba4c96763f..cad67363f2 100644
--- a/pkgs/racket-test/tests/pkg/tests-promote.rkt
+++ b/pkgs/racket-test/tests/pkg/tests-promote.rkt
@@ -15,11 +15,11 @@
     "promote"
     $ "raco pkg config --set catalogs http://localhost:9990"
     $ "raco pkg install --deps search-auto test-pkgs/pkg-test2.zip" =exit> 0
-    $ "raco pkg show -l -u -a" =stdout> #rx"Package\\[\\*=auto\\] +Checksum +Source\npkg-test1\\* +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+tests/pkg/test-pkgs/pkg-test2.zip\"\\)\n"
+    $ "raco pkg show -l -u -a" =stdout> #rx"Package\\[\\*=auto\\] +Checksum +Source\npkg-test1\\* +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+/test-pkgs/pkg-test2.zip\"\\)\n"
     $ "raco pkg install test-pkgs/pkg-test2.zip" =exit> 1 =stderr> #rx"already installed"
     $ "raco pkg install test-pkgs/pkg-test1.zip" =exit> 1 =stderr> #rx"already installed from a different source"
     $ "raco pkg install pkg-test1" ; promote
-    $ "raco pkg show -l -u -a" =stdout> #rx"Package +Checksum +Source\npkg-test1 +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+tests/pkg/test-pkgs/pkg-test2.zip\"\\)\n"
+    $ "raco pkg show -l -u -a" =stdout> #rx"Package +Checksum +Source\npkg-test1 +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+/test-pkgs/pkg-test2.zip\"\\)\n"
     $ "raco pkg install pkg-test1" =exit> 1 =stderr> #rx"already installed" ; redundant promote fails
     $ "racket -e '(require pkg-test1)'" =exit> 0
     $ "racket -e '(require pkg-test2)'" =exit> 0
@@ -32,9 +32,9 @@
     "demote"
     $ "raco pkg config --set catalogs http://localhost:9990"
     $ "raco pkg install --deps search-auto test-pkgs/pkg-test2.zip" =exit> 0
-    $ "raco pkg show -l -u -a" =stdout> #rx"Package\\[\\*=auto\\] +Checksum +Source\npkg-test1\\* +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+tests/pkg/test-pkgs/pkg-test2.zip\"\\)\n"
+    $ "raco pkg show -l -u -a" =stdout> #rx"Package\\[\\*=auto\\] +Checksum +Source\npkg-test1\\* +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+/test-pkgs/pkg-test2.zip\"\\)\n"
     $ "raco pkg remove --demote pkg-test2"
-    $ "raco pkg show -l -u -a" =stdout> #rx"Package\\[\\*=auto\\] +Checksum +Source\npkg-test1\\* +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2\\* +[a-f0-9.]+ +\\(file .+tests/pkg/test-pkgs/pkg-test2.zip\"\\)\n"
+    $ "raco pkg show -l -u -a" =stdout> #rx"Package\\[\\*=auto\\] +Checksum +Source\npkg-test1\\* +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2\\* +[a-f0-9.]+ +\\(file .+/test-pkgs/pkg-test2.zip\"\\)\n"
     $ "racket -e '(require pkg-test1)'" =exit> 0
     $ "racket -e '(require pkg-test2)'" =exit> 0
     $ "raco pkg remove --auto"
@@ -44,8 +44,8 @@
     "demote+auto"
     $ "raco pkg config --set catalogs http://localhost:9990"
     $ "raco pkg install --deps search-auto test-pkgs/pkg-test2.zip" =exit> 0
-    $ "raco pkg show -l -u -a" =stdout> #rx"Package\\[\\*=auto\\] +Checksum +Source\npkg-test1\\* +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+tests/pkg/test-pkgs/pkg-test2.zip\"\\)\n"
+    $ "raco pkg show -l -u -a" =stdout> #rx"Package\\[\\*=auto\\] +Checksum +Source\npkg-test1\\* +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+/test-pkgs/pkg-test2.zip\"\\)\n"
     $ "raco pkg remove --demote --auto pkg-test1" =exit> 0 ; should have no effect
-    $ "raco pkg show -l -u -a" =stdout> #rx"Package\\[\\*=auto\\] +Checksum +Source\npkg-test1\\* +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+tests/pkg/test-pkgs/pkg-test2.zip\"\\)\n"
+    $ "raco pkg show -l -u -a" =stdout> #rx"Package\\[\\*=auto\\] +Checksum +Source\npkg-test1\\* +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+/test-pkgs/pkg-test2.zip\"\\)\n"
     $ "raco pkg remove --demote --auto pkg-test2"
     $ "raco pkg show -l -u -a" =stdout> " [none]\n"))))
diff --git a/pkgs/racket-test/tests/pkg/tests-remove.rkt b/pkgs/racket-test/tests/pkg/tests-remove.rkt
index 073042a613..e9e9a83e62 100644
--- a/pkgs/racket-test/tests/pkg/tests-remove.rkt
+++ b/pkgs/racket-test/tests/pkg/tests-remove.rkt
@@ -35,12 +35,12 @@
                    "pkg-test1 pkg-test1")
    (shelly-install "remove of dep fails"
                    "test-pkgs/pkg-test1.zip"
-                   $ "raco pkg show -l -u -a" =stdout> #rx"Package +Checksum +Source\npkg-test1 +[a-f0-9.]+ +\\(file .+tests/pkg/test-pkgs/pkg-test1.zip\"\\)\n"
+                   $ "raco pkg show -l -u -a" =stdout> #rx"Package +Checksum +Source\npkg-test1 +[a-f0-9.]+ +\\(file .+/test-pkgs/pkg-test1.zip\"\\)\n"
                    $ "raco pkg install test-pkgs/pkg-test2.zip"
-                   $ "raco pkg show -l -u -a" =stdout> #rx"Package +Checksum +Source\npkg-test1 +[a-f0-9.]+ +\\(file .+tests/pkg/test-pkgs/pkg-test1.zip\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+tests/pkg/test-pkgs/pkg-test2.zip\"\\)\n"
+                   $ "raco pkg show -l -u -a" =stdout> #rx"Package +Checksum +Source\npkg-test1 +[a-f0-9.]+ +\\(file .+/test-pkgs/pkg-test1.zip\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+/test-pkgs/pkg-test2.zip\"\\)\n"
                    $ "raco pkg remove pkg-test1" =exit> 1 =stderr> #rx"pkg-test1 \\(required by: \\(pkg-test2\\)\\)"
                    $ "raco pkg remove pkg-test2"
-                   $ "raco pkg show -l -u -a" =stdout>  #rx"Package +Checksum +Source\npkg-test1 +[a-f0-9.]+ +\\(file .+tests/pkg/test-pkgs/pkg-test1.zip\"\\)\n")
+                   $ "raco pkg show -l -u -a" =stdout>  #rx"Package +Checksum +Source\npkg-test1 +[a-f0-9.]+ +\\(file .+/test-pkgs/pkg-test1.zip\"\\)\n")
    (shelly-install "remove of dep can be forced"
                    "test-pkgs/pkg-test1.zip"
                    $ "raco pkg install test-pkgs/pkg-test2.zip"
@@ -68,7 +68,7 @@
      $ "racket -e '(require pkg-test1)'" =exit> 1
      $ "racket -e '(require pkg-test2)'" =exit> 1
      $ "raco pkg install --deps search-auto test-pkgs/pkg-test2.zip" =exit> 0
-     $ "raco pkg show -l -u -a" =stdout> #rx"Package\\[\\*=auto\\] +Checksum +Source\npkg-test1\\* +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+tests/pkg/test-pkgs/pkg-test2.zip\"\\)\n"
+     $ "raco pkg show -l -u -a" =stdout> #rx"Package\\[\\*=auto\\] +Checksum +Source\npkg-test1\\* +[a-f0-9.]+ +\\(catalog \"pkg-test1\"\\)\npkg-test2 +[a-f0-9.]+ +\\(file .+/test-pkgs/pkg-test2.zip\"\\)\n"
      $ "racket -e '(require pkg-test1)'" =exit> 0
      $ "racket -e '(require pkg-test2)'" =exit> 0
      $ "racket -e '(require pkg-test2/contains-dep)'" =exit> 0
diff --git a/pkgs/racket-test/tests/pkg/util.rkt b/pkgs/racket-test/tests/pkg/util.rkt
index 918b6cb754..4e20a99fe9 100644
--- a/pkgs/racket-test/tests/pkg/util.rkt
+++ b/pkgs/racket-test/tests/pkg/util.rkt
@@ -13,7 +13,23 @@
          setup/dirs
          "shelly.rkt")
 
-(define-runtime-path test-directory ".")
+(define-runtime-path test-source-directory ".")
+
+;; Use a consistent directory, so that individual tests can be
+;; run after "tests-create.rkt":
+(define-runtime-path test-directory (build-path (find-system-path 'temp-dir)
+                                                "pkg-test-work"))
+
+(define (sync-test-directory)
+  (printf "Syncing test directory\n")
+  (make-directory* test-directory)
+  (parameterize ([current-directory test-source-directory])
+    (for ([f (in-directory)])
+      (define src f)
+      (define dest (build-path test-directory f))
+      (cond
+       [(directory-exists? src) (make-directory* dest)]
+       [else (copy-file src dest #t)]))))
 
 (define-syntax-rule (this-test-is-run-by-the-main-test)
   (module test racket/base))
@@ -186,6 +202,7 @@
       (shelly-case "setup info cache" $ "raco setup -nDKxiI --no-foreign-libs")
       (with-fake-root
        (parameterize ([current-directory test-directory])
+         (sync-test-directory)
          (t)))))))
 
 (define-syntax-rule (shelly-install** message pkg rm-pkg (pre ...) (more ...))
diff --git a/pkgs/racket-test/tests/racket/old-make-zo.rkt b/pkgs/racket-test/tests/racket/old-make-zo.rkt
index a4c0c34f22..eed95c1cfe 100644
--- a/pkgs/racket-test/tests/racket/old-make-zo.rkt
+++ b/pkgs/racket-test/tests/racket/old-make-zo.rkt
@@ -1,7 +1,14 @@
-#lang racket
-(require setup/dirs)
+#lang racket/base
+(require setup/dirs
+         racket/path
+         racket/file
+         racket/system)
 
-(define src (collection-file-path "old-make-zo.rkt" "tests" "racket"))
+(define orig-src (collection-file-path "old-make-zo.rkt" "tests" "racket"))
+
+(define tmp-dir (make-temporary-file "old-zo~a" 'directory))
+(define src (build-path tmp-dir (file-name-from-path orig-src)))
+(copy-file orig-src src)
 
 (define (exe s)
   (if (eq? (system-type) 'windows)
@@ -28,3 +35,5 @@
           "make"
           "--no-deps"
           (path->string src)))
+
+(delete-directory/files tmp-dir)