Unix/ls: Difference between revisions

Content deleted Content added
→‎{{header|AWK}}: readdir function
→‎{{header|Racket}}: No need to sort files, better looking tests
Line 265:
 
<lang racket>#lang racket/base
(require racket/string racket/file)
 
;; Racket's `directory-list' produces a sorted list of files
(define (ls)
(define (ls) (for-each displayln (directory-list)))
(string-join
(sort (map path->string (directory-list)) string<?)
"\n")))
 
;; Code to run when this file is running directly
(module+ main
(ls))
 
(module+ test
(require tests/eli-tester racket/port racket/file)
(define (make-directory-tree)
(make-directory* "foo/bar")
(for (([f '("1" "2" "a" "b"))])
(with-output-to-file (format "foo/bar/~a"f) #:exists 'replace newline)))
#:exists 'replace newline)))
(make-directory-tree)
(define (ls/str dir)
(test
(parameterize (([current-directory "foo"dir]) (with-output-to-string ls)))
(with-output-to-stringtest (ls/str "foo") => "bar\n")
(parameterize ((current-directoryls/str "foo/bar") => "1\n2\na\nb\n"))</lang>
(with-output-to-string ls) => "1\n2\na\nb\n")))</lang>
 
Both tests pass.