Skip to content

Add specs for File.match? from fast-glob #15604

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

Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ src/openssl/ssl/defaults.cr linguist-generated
src/string/grapheme/properties.cr linguist-generated
# produced by scripts/generate_unicode_data.cr
src/unicode/data.cr linguist-generated
# produced by scripts/generate_glob_specs.cr
spec/std/file/match-fast-glob_spec.cr
# produced by scripts/generate_grapheme_break_specs.cr
spec/std/string/grapheme_break_spec.cr linguist-generated
# produced by spec/generate_wasm32_spec.sh
Expand Down
66 changes: 66 additions & 0 deletions scripts/generate_glob_specs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/env bash

# This tool generates the tests in spec/std/file/match-fast-glob_spec.cr
# from https://github.com/oxc-project/fast-glob/blob/main/tests/test.rs
# converted to Crystal

set -euo pipefail

target=spec/std/file/match-fast-glob_spec.cr
URL="https://raw.githubusercontent.com/oxc-project/fast-glob/refs/heads/main/tests/test.rs"

curl -L "$URL" |
(
echo '
# This file was automatically generated by running:
#
# scripts/generate_glob_specs.cr
#
# DO NOT EDIT
'

echo "
# These tests are autogenerated from $URL
# They are are collection of tests from bash and micromatch
# https://github.com/micromatch/picomatch/blob/master/test/bash.js.
"

echo '
require "spec"

private def assert_file_matches(pattern, path : String, *, file = __FILE__, line = __LINE__)
File.match?(pattern, path).should be_true, file: file, line: line
File.match?(pattern, Path.posix(path)).should be_true, file: file, line: line
File.match?(pattern, Path.posix(path).to_windows(mappings: false)).should be_true, file: file, line: line
end

private def refute_file_matches(pattern, path : String, *, file = __FILE__, line = __LINE__)
File.match?(pattern, path).should be_false, file: file, line: line
File.match?(pattern, Path.posix(path)).should be_false, file: file, line: line
File.match?(pattern, Path.posix(path).to_windows(mappings: false)).should be_false, file: file, line: line
end
'

echo 'describe "File .match? bash tests" do'

sed -E '1,5d' |
sed '/let patterns/,/#\[test\]/d; s/fn not_paired_braces/end\n\nfn not_paired_braces/' | # drop complex patterns implementation
sed '/fn fuzz_tests/,$d' | # drop complex fuzz tests implementation
sed '/^\s*#\[/d' |
sed -E 's/fn (.*)\(\) \{/it "\1" do/' |
sed -E 's/\}$/end/' |
sed -E 's|^\s*//|#|' |
sed -E 's/assert!\(!glob_match\("(.*)", "(.*)"\)\);/refute_file_matches "\1", "\2"/' |
sed -E 's/assert!\(glob_match\("(.*)", "(.*)"\)\);/assert_file_matches "\1", "\2"/' |
# multiline assertions:
sed -E 's/assert!\(glob_match\(/assert_file_matches(/' |
sed -E 's/assert!\(!glob_match\(/refute_file_matches(/' |
sed -E 's/\)\);/)/' |
sed -E 's/let //' |
sed -E 's/it "negation"/pending "negation"/' | # File.match? currently does not support negated patterns
sed -E '/it "generic_input"/,/end$/d' # Generic input tests are specific to Rust types

echo 'end'
) > "$target"

crystal tool format "$target"
Loading
Loading