markdown_exec 2.0.5 → 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/find_files.rb CHANGED
@@ -2,13 +2,13 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  # encoding=utf-8
5
- # version 2024-01-15
5
+ # version 2024-05-24
6
6
 
7
7
  # Finds files matching a given pattern within specified directory paths while optionally excluding
8
8
  # "." and ".." entries and directory names from the results.
9
9
  #
10
- # The function takes a pattern (filename or pattern with wildcards), an array of paths, and an
11
- # option to exclude directory entries and special entries "." and "..".
10
+ # The function takes a pattern (filename or pattern with wildcards), an array of paths, and options
11
+ # to exclude directory entries and special entries "." and "..", and to use relative paths.
12
12
  # It searches for files matching the pattern within each of the specified paths. Hidden files
13
13
  # are included in the search. The search can include subdirectories depending on the
14
14
  # path specification (e.g., 'dir/**' for recursive search).
@@ -18,15 +18,17 @@
18
18
  # paths (Array<String>): An array of directory paths where the search will be performed.
19
19
  # Paths can include wildcards for recursive search.
20
20
  # exclude_dirs (Boolean): If true, excludes "." and ".." and directory names from the results.
21
+ # use_relative_paths (Boolean): If true, removes the app's base directory from the file names
22
+ # if present.
21
23
  #
22
24
  # Returns:
23
25
  # Array<String>: A unique list of file paths that match the given pattern in the specified paths,
24
- # excluding directories if exclude_dirs is true.
26
+ # excluding directories if exclude_dirs is true. Paths are relative if use_relative_paths is true.
25
27
  #
26
28
  # Example:
27
- # find_files('version.rb', ['lib/**', 'spec'], true)
28
- # # This might return file paths like ['lib/markdown_exec/version.rb', 'spec/version_spec.rb'].
29
- def find_files(pattern, paths = ['', Dir.pwd], exclude_dirs: false)
29
+ # find_files('version.rb', ['lib/**', 'spec'], true, true)
30
+ # # This might return file paths like ['markdown_exec/version.rb', 'spec/version_spec.rb'].
31
+ def find_files(pattern, paths = ['', Dir.pwd], base_dir: Dir.pwd, exclude_dirs: false, use_relative_paths: true)
30
32
  matched_files = []
31
33
 
32
34
  paths.each do |path_with_wildcard|
@@ -39,44 +41,15 @@ def find_files(pattern, paths = ['', Dir.pwd], exclude_dirs: false)
39
41
  # Optionally exclude "." and ".." and directory names
40
42
  files.reject! { |file| file.end_with?('/.', '/..') || File.directory?(file) } if exclude_dirs
41
43
 
44
+ # Optionally use relative paths
45
+ files.map! { |file| file.sub(/^#{Regexp.escape(base_dir)}\//, '') } if use_relative_paths
46
+
42
47
  matched_files += files
43
48
  end
44
49
 
45
50
  matched_files.uniq
46
51
  end
47
52
 
48
- # # Finds files matching a given pattern within specified directory paths.
49
- # #
50
- # # The function takes a pattern (filename or pattern with wildcards) and an array of paths.
51
- # # It searches for files matching the pattern within each of the specified paths. Hidden files
52
- # # are also included in the search. The search can include subdirectories depending on the
53
- # # path specification (e.g., 'dir/**' for recursive search).
54
- # #
55
- # # Args:
56
- # # pattern (String): A filename or a pattern string with wildcards.
57
- # # paths (Array<String>): An array of directory paths where the search will be performed.
58
- # # Paths can include wildcards for recursive search.
59
- # #
60
- # # Returns:
61
- # # Array<String>: A unique list of file paths that match the given pattern in the specified paths.
62
- # #
63
- # # Example:
64
- # # find_files('version.rb', ['lib/**', 'spec'])
65
- # # # This might return file paths like ['lib/markdown_exec/version.rb', 'spec/version_spec.rb'].
66
- # def find_files(pattern, paths = ['', Dir.pwd])
67
- # matched_files = []
68
-
69
- # paths.each do |path_with_wildcard|
70
- # # Combine the path with the wildcard and the pattern
71
- # search_pattern = File.join(path_with_wildcard, pattern)
72
-
73
- # # Use Dir.glob with the File::FNM_DOTMATCH flag to include hidden files
74
- # matched_files += Dir.glob(search_pattern, File::FNM_DOTMATCH)
75
- # end
76
-
77
- # matched_files.uniq
78
- # end
79
-
80
53
  return if $PROGRAM_NAME != __FILE__
81
54
 
82
55
  # example CLI
@@ -136,4 +109,27 @@ class TestFindFiles < Minitest::Test
136
109
  result = find_files('.gitignore', ['.'])
137
110
  assert_includes result, './.gitignore'
138
111
  end
112
+
113
+ def test_find_files_with_non_existent_paths
114
+ # Test with non-existent paths
115
+ result = find_files('*.rb', %w[non_existent_dir another_fake_dir])
116
+ assert_empty result
117
+ end
118
+
119
+ def test_find_files_with_mixed_existent_and_non_existent_paths
120
+ # Test with a mix of existing and non-existing paths
121
+ result = find_files('*.rb', %w[lib non_existent_dir])
122
+ assert_includes result, 'lib/cli.rb'
123
+ assert_includes result, 'lib/colorize.rb'
124
+ # Ensure that non-existent paths do not cause failure and do not include files
125
+ assert_equal result.length, Dir.glob('lib/*.rb').length
126
+ end
127
+
128
+ def test_find_files_with_relative_paths
129
+ # Test with relative paths
130
+ base_dir = Dir.pwd
131
+ result = find_files('cli.rb', ['lib'], use_relative_paths: true)
132
+ assert_includes result, 'lib/cli.rb'
133
+ refute_includes result, "#{base_dir}/lib/cli.rb"
134
+ end
139
135
  end