Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'rglob' function to stdlib. #242

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions stdlib/rglob.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
fn dir_glob(dir, pattern, results) {
files <= find $dir -maxdepth 1 -mindepth 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdlib functions should never abort the script. We must treat (or ignore in some cases) all errors.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can optimize this a lot passing -type d to get only the directories.

files <= split($files, "\n")

for f in $files {
_, status <= test -d $f
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if asking only directories to find then this check could be skipped.
What do you think?


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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this is the right way to handle the rglob. It will only work in the current directory? I'm not saying we must add support to the '/path/**' syntax, but maybe the signature could be rglob(dir, pattern).
What do you guys think?

result <= glob($working_dir+"/"+$pattern)
result <= dir_glob($working_dir, $pattern, $result)

return $result
}
85 changes: 85 additions & 0 deletions stdlib/rglob_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env nash

import rglob

fn setup() {
temp_dir <= mktemp -d
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this directory is leaking.


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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kkkkkkk

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kkkkk

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to return the cleaning function also, something like:

       fn clean() {
	    rm -rf $temp_dir
        }

	return $temp_dir, $clean

}

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)