markdown_exec 1.8.7 → 1.8.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/find_files.rb ADDED
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/env bundle exec ruby
2
+ # frozen_string_literal: true
3
+
4
+ # encoding=utf-8
5
+ # version 2024-01-15
6
+
7
+ # Finds files matching a given pattern within specified directory paths.
8
+ #
9
+ # The function takes a pattern (filename or pattern with wildcards) and an array of paths.
10
+ # It searches for files matching the pattern within each of the specified paths. Hidden files
11
+ # are also included in the search. The search can include subdirectories depending on the
12
+ # path specification (e.g., 'dir/**' for recursive search).
13
+ #
14
+ # Args:
15
+ # pattern (String): A filename or a pattern string with wildcards.
16
+ # paths (Array<String>): An array of directory paths where the search will be performed.
17
+ # Paths can include wildcards for recursive search.
18
+ #
19
+ # Returns:
20
+ # Array<String>: A unique list of file paths that match the given pattern in the specified paths.
21
+ #
22
+ # Example:
23
+ # find_files('version.rb', ['lib/**', 'spec'])
24
+ # # This might return file paths like ['lib/markdown_exec/version.rb', 'spec/version_spec.rb'].
25
+ def find_files(pattern, paths)
26
+ matched_files = []
27
+
28
+ paths.each do |path_with_wildcard|
29
+ # Combine the path with the wildcard and the pattern
30
+ search_pattern = File.join(path_with_wildcard, pattern)
31
+
32
+ # Use Dir.glob with the File::FNM_DOTMATCH flag to include hidden files
33
+ matched_files += Dir.glob(search_pattern, File::FNM_DOTMATCH)
34
+ end
35
+
36
+ matched_files.uniq
37
+ end
38
+
39
+ return if $PROGRAM_NAME != __FILE__
40
+
41
+ # example CLI
42
+ # ruby lib/find_files.rb import1.md 'spec:fixtures/**:examples/**' ':'
43
+
44
+ if ARGV.length.positive?
45
+ pattern, path_list, sep = ARGV
46
+ puts find_files(pattern, path_list.split(sep || ':'))
47
+
48
+ return
49
+ end
50
+
51
+ require 'bundler/setup'
52
+ Bundler.require(:default)
53
+
54
+ require 'minitest/autorun'
55
+
56
+ class TestFindFiles < Minitest::Test
57
+ def test_find_files_no_recursion
58
+ # Test with no recursive directories
59
+ result = find_files('cli.rb', ['lib'])
60
+ assert_includes result, 'lib/cli.rb'
61
+ end
62
+
63
+ def test_find_files_with_recursion
64
+ # Test with recursive directories
65
+ expected_files = [
66
+ 'lib/cli.rb',
67
+ 'lib/colorize.rb',
68
+ 'lib/dev/llm.rb',
69
+ 'lib/dev/watchfile.sh',
70
+ 'lib/markdown_exec.rb',
71
+ 'lib/markdown_exec/version.rb'
72
+ ]
73
+ result = find_files('*', ['lib/**'])
74
+ expected_files.each do |file|
75
+ assert_includes result, file
76
+ end
77
+ end
78
+
79
+ def test_find_files_in_multiple_paths
80
+ # Test with multiple paths
81
+ expected_files = [
82
+ 'lib/markdown_exec/version.rb',
83
+ 'spec/cli_spec.rb',
84
+ 'spec/env_spec.rb',
85
+ 'spec/markdown_exec_spec.rb',
86
+ 'spec/tap_spec.rb'
87
+ ]
88
+ result = find_files('*', ['lib/**', 'spec'])
89
+ expected_files.each do |file|
90
+ assert_includes result, file
91
+ end
92
+ end
93
+
94
+ def test_find_files_with_hidden_files
95
+ # Test to ensure hidden files are also found
96
+ result = find_files('.gitignore', ['.'])
97
+ assert_includes result, './.gitignore'
98
+ end
99
+ end