From 81b41fe17d157aa31503c4f75729e5f82690a37e Mon Sep 17 00:00:00 2001 From: Vitor Arins Date: Thu, 28 Sep 2017 16:44:09 -0300 Subject: [PATCH 1/2] Added 'rglob' function to stdlib. --- stdlib/rglob.sh | 29 +++++++++++++++ stdlib/rglob_test.sh | 85 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 stdlib/rglob.sh create mode 100755 stdlib/rglob_test.sh diff --git a/stdlib/rglob.sh b/stdlib/rglob.sh new file mode 100644 index 00000000..2eab71bb --- /dev/null +++ b/stdlib/rglob.sh @@ -0,0 +1,29 @@ +fn dir_glob(dir, pattern, results) { + files <= find $dir -maxdepth 1 -mindepth 1 + files <= split($files, "\n") + + for f in $files { + _, status <= test -d $f + + if $status == "0" { + dir_pattern <= format("%s/%s", $f, $pattern) + current_results <= glob($dir_pattern) + + for r in $current_results { + results <= append($results, $r) + } + + results <= dir_glob($f, $pattern, $results) + } + } + + return $results +} + +fn rglob(pattern) { + working_dir <= pwd + result <= glob($working_dir+"/"+$pattern) + result <= dir_glob($working_dir, $pattern, $result) + + return $result +} diff --git a/stdlib/rglob_test.sh b/stdlib/rglob_test.sh new file mode 100755 index 00000000..bb8e73e8 --- /dev/null +++ b/stdlib/rglob_test.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env nash + +import rglob + +fn setup() { + temp_dir <= mktemp -d + + test_txt = $temp_dir+"/test.txt" + test_sh = $temp_dir+"/test.sh" + + _, _ <= touch $test_txt + _, _ <= touch $test_sh + + hello_dir = $temp_dir+"/hello" + world_dir = $temp_dir+"/world" + + _, _ <= mkdir $hello_dir + _, _ <= mkdir $world_dir + + honda_txt = $hello_dir+"/honda.txt" + honda_sh = $hello_dir+"/honda.sh" + civic_txt = $world_dir+"/civic.txt" + civic_sh = $world_dir+"/civic.sh" + + _, _ <= touch $honda_txt + _, _ <= touch $honda_sh + _, _ <= touch $civic_txt + _, _ <= touch $civic_sh + + return $temp_dir +} + +fn assert_success(expected, got) { + if len($got) != len($expected) { + print("expected length to be '%s' but got '%s'\n", len($expected), len($got)) + exit("1") + } + + found = "0" + + for x in $expected { + for y in $got { + if $x == $y { + found <= echo $found+" + 1" | bc + } + } + } + + if $found != len($expected) { + print("expected [%s] got [%s]\n", $expected, $got) + exit("1") + } +} + +fn test_rglob_txt(dir) { + expected_files = ( + $dir+"/test.txt" + $dir+"/hello/honda.txt" + $dir+"/world/civic.txt" + ) + + got_files <= rglob("*.txt") + + assert_success($expected_files, $got_files) +} + +fn test_rglob_sh(dir) { + expected_files = ( + $dir+"/test.sh" + $dir+"/hello/honda.sh" + $dir+"/world/civic.sh" + ) + + got_files <= rglob("*.sh") + + assert_success($expected_files, $got_files) +} + +old_dir <= pwd +temp_dir <= setup() + +chdir($temp_dir) +test_rglob_txt($temp_dir) +test_rglob_sh($temp_dir) +chdir($old_dir) From 78de90848d4900eaa04047b61d582deb7f84e0a7 Mon Sep 17 00:00:00 2001 From: Vitor Arins Date: Thu, 28 Sep 2017 18:12:19 -0300 Subject: [PATCH 2/2] Removed unnecessary code from test. --- stdlib/rglob_test.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/stdlib/rglob_test.sh b/stdlib/rglob_test.sh index bb8e73e8..235e9067 100755 --- a/stdlib/rglob_test.sh +++ b/stdlib/rglob_test.sh @@ -5,27 +5,27 @@ import rglob fn setup() { temp_dir <= mktemp -d - test_txt = $temp_dir+"/test.txt" - test_sh = $temp_dir+"/test.sh" + test_txt = $temp_dir+"/test.txt" + test_sh = $temp_dir+"/test.sh" - _, _ <= touch $test_txt - _, _ <= touch $test_sh + touch $test_txt + touch $test_sh hello_dir = $temp_dir+"/hello" world_dir = $temp_dir+"/world" - _, _ <= mkdir $hello_dir - _, _ <= mkdir $world_dir + mkdir $hello_dir + mkdir $world_dir honda_txt = $hello_dir+"/honda.txt" honda_sh = $hello_dir+"/honda.sh" civic_txt = $world_dir+"/civic.txt" civic_sh = $world_dir+"/civic.sh" - _, _ <= touch $honda_txt - _, _ <= touch $honda_sh - _, _ <= touch $civic_txt - _, _ <= touch $civic_sh + touch $honda_txt + touch $honda_sh + touch $civic_txt + touch $civic_sh return $temp_dir }