|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2025, the original author(s). |
| 3 | + * |
| 4 | + * This software is distributable under the BSD license. See the terms of the |
| 5 | + * BSD license in the documentation provided with this software. |
| 6 | + * |
| 7 | + * https://opensource.org/licenses/BSD-3-Clause |
| 8 | + */ |
| 9 | +package org.jline.builtins; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.nio.file.FileSystem; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.Arguments; |
| 23 | +import org.junit.jupiter.params.provider.MethodSource; |
| 24 | + |
| 25 | +import com.google.common.jimfs.Configuration; |
| 26 | +import com.google.common.jimfs.Jimfs; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.*; |
| 29 | + |
| 30 | +/** |
| 31 | + * Test SyntaxHighlighter.addFiles() with JimFS to verify cross-platform behavior |
| 32 | + * on both Unix and Windows file systems. |
| 33 | + */ |
| 34 | +public class SyntaxHighlighterJimFsTest { |
| 35 | + |
| 36 | + private static Stream<Arguments> fileSystemConfigurations() { |
| 37 | + return Stream.of(Arguments.of("Unix", Configuration.unix()), Arguments.of("Windows", Configuration.windows())); |
| 38 | + } |
| 39 | + |
| 40 | + @ParameterizedTest(name = "{0} file system") |
| 41 | + @MethodSource("fileSystemConfigurations") |
| 42 | + public void testRelativeRecursiveGlobPattern(String fsName, Configuration config) throws IOException { |
| 43 | + try (FileSystem fs = Jimfs.newFileSystem(config)) { |
| 44 | + // Create nested directory structure: foo/bar/baz/ |
| 45 | + Path root = getFileSystemRoot(fs, config); |
| 46 | + Path fooDir = root.resolve("foo"); |
| 47 | + Path barDir = fooDir.resolve("bar"); |
| 48 | + Path bazDir = barDir.resolve("baz"); |
| 49 | + Files.createDirectories(bazDir); |
| 50 | + |
| 51 | + // Create test files in different levels |
| 52 | + Path file1 = barDir.resolve("test1.nanorc"); |
| 53 | + Path file2 = bazDir.resolve("test2.nanorc"); |
| 54 | + Path file3 = bazDir.resolve("other.txt"); |
| 55 | + |
| 56 | + Files.write(file1, "syntax test1".getBytes(StandardCharsets.UTF_8)); |
| 57 | + Files.write(file2, "syntax test2".getBytes(StandardCharsets.UTF_8)); |
| 58 | + Files.write(file3, "not a nanorc file".getBytes(StandardCharsets.UTF_8)); |
| 59 | + |
| 60 | + // Create nanorc file in the root |
| 61 | + Path nanorc = root.resolve("jnanorc"); |
| 62 | + Files.write(nanorc, "# test config".getBytes(StandardCharsets.UTF_8)); |
| 63 | + |
| 64 | + // Test relative recursive glob pattern "foo/bar/**/*.nanorc" |
| 65 | + String relativeRecursivePattern = "foo/bar/**/*.nanorc"; |
| 66 | + List<Path> foundFiles = new ArrayList<>(); |
| 67 | + |
| 68 | + SyntaxHighlighter.addFiles(nanorc, relativeRecursivePattern, stream -> stream.forEach(foundFiles::add)); |
| 69 | + |
| 70 | + assertEquals(1, foundFiles.size(), "Should find exactly 2 .nanorc files in foo/bar/**"); |
| 71 | + assertFalse(foundFiles.contains(file1), "Should contain test1.nanorc from foo/bar/"); |
| 72 | + assertTrue(foundFiles.contains(file2), "Should contain test2.nanorc from foo/bar/baz/"); |
| 73 | + assertFalse(foundFiles.contains(file3), "Should not contain other.txt"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private Path getFileSystemRoot(FileSystem fs, Configuration config) { |
| 78 | + if (config == Configuration.windows()) { |
| 79 | + return fs.getPath("C:\\"); |
| 80 | + } else { |
| 81 | + return fs.getPath("/"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @ParameterizedTest(name = "{0} file system") |
| 86 | + @MethodSource("fileSystemConfigurations") |
| 87 | + public void testAbsoluteRecursiveGlobPattern(String fsName, Configuration config) throws IOException { |
| 88 | + try (FileSystem fs = Jimfs.newFileSystem(config)) { |
| 89 | + // Create nested directory structure |
| 90 | + Path root = getFileSystemRoot(fs, config); |
| 91 | + Path shareDir, configDir; |
| 92 | + String absoluteRecursivePattern; |
| 93 | + |
| 94 | + if (config == Configuration.windows()) { |
| 95 | + shareDir = root.resolve("Program Files").resolve("nano"); |
| 96 | + configDir = root.resolve("config"); |
| 97 | + absoluteRecursivePattern = "C:/Program Files/nano/**/*.nanorc"; |
| 98 | + } else { |
| 99 | + shareDir = root.resolve("usr").resolve("share").resolve("nano"); |
| 100 | + configDir = root.resolve("etc"); |
| 101 | + absoluteRecursivePattern = "/usr/share/nano/**/*.nanorc"; |
| 102 | + } |
| 103 | + |
| 104 | + Path subDir = shareDir.resolve("subdir"); |
| 105 | + Files.createDirectories(subDir); |
| 106 | + Files.createDirectories(configDir); |
| 107 | + |
| 108 | + // Create test files |
| 109 | + Path file1 = shareDir.resolve("test1.nanorc"); |
| 110 | + Path file2 = subDir.resolve("test2.nanorc"); |
| 111 | + |
| 112 | + Files.write(file1, "syntax test1".getBytes(StandardCharsets.UTF_8)); |
| 113 | + Files.write(file2, "syntax test2".getBytes(StandardCharsets.UTF_8)); |
| 114 | + |
| 115 | + // Create nanorc file in a different location |
| 116 | + Path nanorc = configDir.resolve("jnanorc"); |
| 117 | + Files.write(nanorc, "# test config".getBytes(StandardCharsets.UTF_8)); |
| 118 | + |
| 119 | + // Test absolute recursive glob pattern |
| 120 | + List<Path> foundFiles = new ArrayList<>(); |
| 121 | + |
| 122 | + SyntaxHighlighter.addFiles(nanorc, absoluteRecursivePattern, stream -> stream.forEach(foundFiles::add)); |
| 123 | + |
| 124 | + assertEquals(1, foundFiles.size(), "Should find exactly 2 .nanorc files recursively"); |
| 125 | + assertFalse(foundFiles.contains(file1), "Should contain test1.nanorc"); |
| 126 | + assertTrue(foundFiles.contains(file2), "Should contain test2.nanorc"); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @ParameterizedTest(name = "{0} file system") |
| 131 | + @MethodSource("fileSystemConfigurations") |
| 132 | + public void testSimpleGlobPattern(String fsName, Configuration config) throws IOException { |
| 133 | + try (FileSystem fs = Jimfs.newFileSystem(config)) { |
| 134 | + Path root = getFileSystemRoot(fs, config); |
| 135 | + |
| 136 | + // Create test files in the root |
| 137 | + Path file1 = root.resolve("test1.nanorc"); |
| 138 | + Path file2 = root.resolve("test2.nanorc"); |
| 139 | + Path file3 = root.resolve("other.txt"); |
| 140 | + |
| 141 | + Files.write(file1, "syntax test1".getBytes(StandardCharsets.UTF_8)); |
| 142 | + Files.write(file2, "syntax test2".getBytes(StandardCharsets.UTF_8)); |
| 143 | + Files.write(file3, "not a nanorc file".getBytes(StandardCharsets.UTF_8)); |
| 144 | + |
| 145 | + // Create nanorc file |
| 146 | + Path nanorc = root.resolve("jnanorc"); |
| 147 | + Files.write(nanorc, "# test config".getBytes(StandardCharsets.UTF_8)); |
| 148 | + |
| 149 | + // Test simple glob pattern "*.nanorc" |
| 150 | + String simplePattern = "*.nanorc"; |
| 151 | + List<Path> foundFiles = new ArrayList<>(); |
| 152 | + |
| 153 | + SyntaxHighlighter.addFiles(nanorc, simplePattern, stream -> stream.forEach(foundFiles::add)); |
| 154 | + |
| 155 | + assertEquals(2, foundFiles.size(), "Should find exactly 2 .nanorc files"); |
| 156 | + assertTrue(foundFiles.contains(file1), "Should contain test1.nanorc"); |
| 157 | + assertTrue(foundFiles.contains(file2), "Should contain test2.nanorc"); |
| 158 | + assertFalse(foundFiles.contains(file3), "Should not contain other.txt"); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @ParameterizedTest(name = "{0} file system") |
| 163 | + @MethodSource("fileSystemConfigurations") |
| 164 | + public void testNonExistentDirectory(String fsName, Configuration config) throws IOException { |
| 165 | + try (FileSystem fs = Jimfs.newFileSystem(config)) { |
| 166 | + Path root = getFileSystemRoot(fs, config); |
| 167 | + |
| 168 | + // Create nanorc file |
| 169 | + Path nanorc = root.resolve("jnanorc"); |
| 170 | + Files.write(nanorc, "# test config".getBytes(StandardCharsets.UTF_8)); |
| 171 | + |
| 172 | + // Test glob pattern pointing to non-existent directory |
| 173 | + String nonExistentPattern = "nonexistent/**/*.nanorc"; |
| 174 | + List<Path> foundFiles = new ArrayList<>(); |
| 175 | + |
| 176 | + // Should not throw an exception |
| 177 | + assertDoesNotThrow(() -> { |
| 178 | + SyntaxHighlighter.addFiles(nanorc, nonExistentPattern, stream -> stream.forEach(foundFiles::add)); |
| 179 | + }); |
| 180 | + |
| 181 | + assertEquals(0, foundFiles.size(), "Should find no files when directory doesn't exist"); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @ParameterizedTest(name = "{0} file system") |
| 186 | + @MethodSource("fileSystemConfigurations") |
| 187 | + public void testNonGlobPattern(String fsName, Configuration config) throws IOException { |
| 188 | + try (FileSystem fs = Jimfs.newFileSystem(config)) { |
| 189 | + Path root = getFileSystemRoot(fs, config); |
| 190 | + |
| 191 | + // Create test file |
| 192 | + Path testFile = root.resolve("test.nanorc"); |
| 193 | + Files.write(testFile, "syntax test".getBytes(StandardCharsets.UTF_8)); |
| 194 | + |
| 195 | + // Create nanorc file |
| 196 | + Path nanorc = root.resolve("jnanorc"); |
| 197 | + Files.write(nanorc, "# test config".getBytes(StandardCharsets.UTF_8)); |
| 198 | + |
| 199 | + // Test non-glob pattern (should work as before) |
| 200 | + String simplePattern = "test.nanorc"; |
| 201 | + List<Path> foundFiles = new ArrayList<>(); |
| 202 | + |
| 203 | + SyntaxHighlighter.addFiles(nanorc, simplePattern, stream -> stream.forEach(foundFiles::add)); |
| 204 | + |
| 205 | + assertEquals(1, foundFiles.size(), "Should find exactly 1 file"); |
| 206 | + assertTrue(foundFiles.contains(testFile), "Should contain test.nanorc"); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + public void testExtractStaticPathPrefix() { |
| 212 | + // Test the extractStaticPathPrefix method indirectly by verifying behavior |
| 213 | + try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) { |
| 214 | + Path root = fs.getPath("/"); |
| 215 | + |
| 216 | + // Create a complex directory structure |
| 217 | + Path deepDir = root.resolve("a").resolve("b").resolve("c").resolve("d"); |
| 218 | + Files.createDirectories(deepDir); |
| 219 | + |
| 220 | + Path testFile = deepDir.resolve("test.nanorc"); |
| 221 | + Files.write(testFile, "syntax test".getBytes(StandardCharsets.UTF_8)); |
| 222 | + |
| 223 | + Path nanorc = root.resolve("jnanorc"); |
| 224 | + Files.write(nanorc, "# test config".getBytes(StandardCharsets.UTF_8)); |
| 225 | + |
| 226 | + // Test pattern "a/b/**/*.nanorc" - should extract "a/b" as static prefix |
| 227 | + String pattern = "a/b/**/*.nanorc"; |
| 228 | + List<Path> foundFiles = new ArrayList<>(); |
| 229 | + |
| 230 | + SyntaxHighlighter.addFiles(nanorc, pattern, stream -> stream.forEach(foundFiles::add)); |
| 231 | + |
| 232 | + assertEquals(1, foundFiles.size(), "Should find the file in the deep directory"); |
| 233 | + assertTrue(foundFiles.contains(testFile), "Should contain the test file"); |
| 234 | + } catch (IOException e) { |
| 235 | + fail("Test should not throw IOException: " + e.getMessage()); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments