(module zip mzscheme (require (planet "io.ss" ("dherman" "io.plt" 1 1))) (require (planet "file.ss" ("dherman" "io.plt" 1 1))) (require (planet "test.ss" ("schematics" "schemeunit.plt" 1))) (require (planet "util.ss" ("schematics" "schemeunit.plt" 1))) (require (planet "text-ui.ss" ("schematics" "schemeunit.plt" 1))) (require (lib "etc.ss")) (require "../../zip.ss") (require "../../unzip.ss") (require "util.ss") (define-syntax in-this-directory (syntax-rules () [(_ e1 e2 ...) (parameterize ([current-directory (this-expression-source-directory)]) e1 e2 ...)])) (define-syntax in-directory (syntax-rules () [(_ dir-e delete-when-done? e1 e2 ...) (let ([dir dir-e]) (rm-rf dir) (make-directory dir) (let ([result (parameterize ([current-directory dir]) e1 e2 ...)]) (when delete-when-done? (rm-rf dir)) result))])) (define gettysburg-address '("Four score and seven years ago our fathers brought forth" "on this continent, a new nation, conceived in Liberty," "and dedicated to the proposition that all men are created" "equal." "" "Now we are engaged in a great civil war, testing whether" "that nation, or any nation so conceived and so dedicated," "can long endure. We are met on a great battle-field of" "that war. We have come to dedicate a portion of that field," "as a final resting place for those who here gave their" "lives that that nation might live. It is altogether" "fitting and proper that we should do this." "" "But, in a larger sense, we can not dedicate -- we can not" "consecrate -- we can not hallow -- this ground. The brave" "men, living and dead, who struggled here, have consecrated" "it, far above our poor power to add or detract. The world" "will little note, nor long remember what we say here, but" "it can never forget what they did here. It is for us the" "living, rather, to be dedicated here to the unfinished" "work which they who fought here have thus far so nobly" "advanced. It is rather for us to be here dedicated to the" "great task remaining before us -- that from these honored" "dead we take increased devotion to that cause for which" "they gave the last full measure of devotion -- that we" "here highly resolve that these dead shall not have died" "in vain -- that this nation, under God, shall have a new" "birth of freedom -- and that government of the people, by" "the people, for the people, shall not perish from the" "earth.")) (define-syntax with-gettysburg-address (syntax-rules () [(_ file e1 e2 ...) (with-temporary-file file ("abe~a.txt" #f (current-directory)) (with-output-to-file file (lambda () (write-lines gettysburg-address)) 'replace) e1 e2 ...)])) ;; gettysburg-address? : (union string bytes path input-port (listof string)) -> boolean (define (gettysburg-address? thing) (cond [(string? thing) (gettysburg-address? (string->path thing))] [(bytes? thing) (gettysburg-address? (bytes->path thing))] [(path? thing) (gettysburg-address? (with-input-from-file thing read-lines))] [(input-port? thing) (gettysburg-address? (read-lines thing))] [(list? thing) (equal? thing gettysburg-address)] [else #f])) (define ex:multiple-files/no-directories '((file 1) (file 2) (file 3) (file 4) (file 5) (dir "empty"))) (define ex:multiple-files/subdirectories '(dir "test-zip" (file 1) (file 2) (file 3) (dir "subdirectory" (file 4) (file 5)))) (define test:zip (make-test-suite "zip tests" (make-test-case "one file, no directories" (in-directory "sandbox" #t (let ([abe.txt/actual (string->path "abe-actual.txt")]) (with-gettysburg-address abe.txt (let ([expected (with-input-from-file abe.txt read-lines)]) (with-temporary-file abe.zip ("abe~a.zip" #f (current-directory)) (with-output-to-file abe.zip (lambda () (zip (list (path->relative-path abe.txt)))) 'replace) (with-input-from-file abe.zip (lambda () (unzip (current-input-port) (lambda (entry-name) (open-output-file abe.txt/actual)) #t))) (assert-true (gettysburg-address? abe.txt/actual)))))))) ;; TODO: test multiple files, no directories (make-test-case "multiple files, no directories" (in-directory "sandbox" #f (in-directory "expected" #f (build-dir-tree ex:multiple-files/no-directories)) ;(fprintf (current-error-port) "~v~n" (all-files (build-path "expected"))) (with-output-to-file "actual.zip" (lambda () (zip (all-files (build-path "expected"))))) (in-directory "actual" #f ()) (assert-true #f) )) ;; TODO: test directories, no files ;; TODO: test mixed files and subdirectories ;; TODO: test on a piped output stream, where I can't seek )) (define zip-tests (make-test-suite "All zip.ss tests" test:zip )) (test/text-ui zip-tests) (provide zip-tests))