-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement standard library glob function (#511)
* Reinstate command to remove /tmp directory. * Add glob functions to standard library. * Whitespace changes. * Reinstate removed blank lines.
- Loading branch information
Showing
13 changed files
with
477 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,5 @@ main { | |
} else { | ||
echo "failed" | ||
} | ||
trust $rm {tmpdir}$ | ||
trust $rm -fr {tmpdir}$ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * from "std/fs" | ||
|
||
// Output | ||
// FAILED | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust { | ||
$ touch {tmpdir}/1st\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/2nd\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/file.txt $ | ||
$ touch {tmpdir}/file1.txt $ | ||
$ touch {tmpdir}/file2.txt $ | ||
$ touch {tmpdir}/file99.txt $ | ||
$ touch {tmpdir}/other.csv $ | ||
} | ||
|
||
let files = glob("{tmpdir}/missing*") failed { | ||
echo "FAILED" | ||
} | ||
loop file in files { | ||
echo "[{file}]" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * from "std/array" | ||
import * from "std/fs" | ||
import * from "std/text" | ||
|
||
fun compare(actual: [Text], expected: [Text]): Bool { | ||
if len(actual) != len(expected) { | ||
return false | ||
} | ||
loop file in expected { | ||
if not includes(actual, file) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust { | ||
$ touch {tmpdir}/1st\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/2nd\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/file.txt $ | ||
$ touch {tmpdir}/file1.txt $ | ||
$ touch {tmpdir}/file2.txt $ | ||
$ touch {tmpdir}/file99.txt $ | ||
$ touch {tmpdir}/other.csv $ | ||
} | ||
|
||
let expected = [ | ||
"{tmpdir}/file.txt", | ||
"{tmpdir}/file1.txt", | ||
"{tmpdir}/file2.txt", | ||
"{tmpdir}/file99.txt", | ||
"{tmpdir}/other.csv", | ||
] | ||
let actual = glob_multiple(["{tmpdir}/missing*", "{tmpdir}/file*.txt", "{tmpdir}/other*.csv"]) failed { | ||
echo "FAILED" | ||
} | ||
if compare(actual, expected) { | ||
echo "Succeded" | ||
} else { | ||
echo "Expected: {expected}" | ||
echo "Actual: {actual}" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * from "std/array" | ||
import * from "std/fs" | ||
import * from "std/text" | ||
|
||
fun compare(actual: [Text], expected: [Text]): Bool { | ||
if len(actual) != len(expected) { | ||
return false | ||
} | ||
loop file in expected { | ||
if not includes(actual, file) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust { | ||
$ touch {tmpdir}/1st\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/2nd\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/file.txt $ | ||
$ touch {tmpdir}/file1.txt $ | ||
$ touch {tmpdir}/file2.txt $ | ||
$ touch {tmpdir}/file99.txt $ | ||
$ touch {tmpdir}/other.csv $ | ||
} | ||
|
||
let expected = [ | ||
"{tmpdir}/file1.txt", | ||
"{tmpdir}/file2.txt", | ||
] | ||
let actual = glob("{tmpdir}/file?.txt") failed { | ||
echo "FAILED" | ||
} | ||
if compare(actual, expected) { | ||
echo "Succeded" | ||
} else { | ||
echo "Expected: {expected}" | ||
echo "Actual: {actual}" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * from "std/array" | ||
import * from "std/fs" | ||
import * from "std/text" | ||
|
||
fun compare(actual: [Text], expected: [Text]): Bool { | ||
if len(actual) != len(expected) { | ||
return false | ||
} | ||
loop file in expected { | ||
if not includes(actual, file) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust { | ||
$ touch {tmpdir}/1st\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/2nd\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/file.txt $ | ||
$ touch {tmpdir}/file1.txt $ | ||
$ touch {tmpdir}/file2.txt $ | ||
$ touch {tmpdir}/file99.txt $ | ||
$ touch {tmpdir}/other.csv $ | ||
} | ||
|
||
let expected = [ | ||
"{tmpdir}/file.txt", | ||
"{tmpdir}/file1.txt", | ||
"{tmpdir}/file2.txt", | ||
"{tmpdir}/file99.txt", | ||
] | ||
let actual = glob("{tmpdir}/file*.txt") failed { | ||
echo "FAILED" | ||
} | ||
if compare(actual, expected) { | ||
echo "Succeded" | ||
} else { | ||
echo "Expected: {expected}" | ||
echo "Actual: {actual}" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * from "std/array" | ||
import * from "std/fs" | ||
import * from "std/text" | ||
|
||
fun compare(actual: [Text], expected: [Text]): Bool { | ||
if len(actual) != len(expected) { | ||
return false | ||
} | ||
loop file in expected { | ||
if not includes(actual, file) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust { | ||
$ touch {tmpdir}/1st\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/2nd\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/file.txt $ | ||
$ touch {tmpdir}/file1.txt $ | ||
$ touch {tmpdir}/file2.txt $ | ||
$ touch {tmpdir}/file99.txt $ | ||
$ touch {tmpdir}/other.csv $ | ||
} | ||
|
||
let expected = [ | ||
"{tmpdir}/1st file with spaces.txt", | ||
"{tmpdir}/2nd file with spaces.txt", | ||
] | ||
let actual = glob("{tmpdir}/*with spaces*") failed { | ||
echo "FAILED" | ||
} | ||
if compare(actual, expected) { | ||
echo "Succeded" | ||
} else { | ||
echo "Expected: {expected}" | ||
echo "Actual: {actual}" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * from "std/fs" | ||
|
||
// Output | ||
// [xxx; do echo HACKED; done; for file in] | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust { | ||
$ touch "{tmpdir}/xxx; do echo HACKED; done; for file in" $ | ||
} | ||
cd tmpdir | ||
|
||
// The glob function escapes all characters in the passed-in glob | ||
// apart from "*", "?" and "/", to prevent injection attacks. If we | ||
// didn't do this, the following code would output "[HACKED]" instead | ||
// of the filename. | ||
let files = glob("xxx; do echo HACKED; done; for file in") failed { | ||
echo "FAILED" | ||
} | ||
loop file in files { | ||
echo "[{file}]" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * from "std/fs" | ||
|
||
// Output | ||
// FAILED | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust { | ||
$ touch {tmpdir}/1st\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/2nd\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/file.txt $ | ||
$ touch {tmpdir}/file1.txt $ | ||
$ touch {tmpdir}/file2.txt $ | ||
$ touch {tmpdir}/file99.txt $ | ||
$ touch {tmpdir}/other.csv $ | ||
} | ||
cd tmpdir | ||
|
||
let files = glob("missing*") failed { | ||
echo "FAILED" | ||
} | ||
loop file in files { | ||
echo "[{file}]" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * from "std/array" | ||
import * from "std/fs" | ||
import * from "std/text" | ||
|
||
fun compare(actual: [Text], expected: [Text]): Bool { | ||
if len(actual) != len(expected) { | ||
return false | ||
} | ||
loop file in expected { | ||
if not includes(actual, file) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
main { | ||
let tmpdir = trust $ mktemp -d $ | ||
trust { | ||
$ touch {tmpdir}/1st\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/2nd\ file\ with\ spaces.txt $ | ||
$ touch {tmpdir}/file.txt $ | ||
$ touch {tmpdir}/file1.txt $ | ||
$ touch {tmpdir}/file2.txt $ | ||
$ touch {tmpdir}/file99.txt $ | ||
$ touch {tmpdir}/other.csv $ | ||
} | ||
cd tmpdir | ||
|
||
let expected = [ | ||
"file.txt", | ||
"file1.txt", | ||
"file2.txt", | ||
"file99.txt", | ||
"other.csv", | ||
] | ||
let actual = glob_multiple(["missing*", "file*.txt", "other*.csv"]) failed { | ||
echo "FAILED" | ||
} | ||
if compare(actual, expected) { | ||
echo "Succeded" | ||
} else { | ||
echo "Expected: {expected}" | ||
echo "Actual: {actual}" | ||
} | ||
|
||
trust $ rm -rf {tmpdir} $ | ||
} |
Oops, something went wrong.