|
| 1 | +/** |
| 2 | + * Community Rust Plugin |
| 3 | + * Copyright (C) 2021-2023 Eric Le Goff |
| 4 | + * mailto:community-rust AT pm DOT me |
| 5 | + * http://github.com/elegoff/sonar-rust |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or |
| 8 | + * modify it under the terms of the GNU Lesser General Public |
| 9 | + * License as published by the Free Software Foundation; either |
| 10 | + * version 3 of the License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | + */ |
| 21 | +package org.elegoff.plugins.communityrust.clippy; |
| 22 | + |
| 23 | + |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.nio.file.Paths; |
| 26 | +import java.util.Arrays; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.sonar.api.batch.fs.internal.TestInputFileBuilder; |
| 30 | +import org.sonar.api.batch.sensor.internal.SensorContextTester; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +public class FileAdjustorTest { |
| 35 | + |
| 36 | + private static final Path PROJECT_DIR = Paths.get("src", "test", "resources", "FileAdjustor"); |
| 37 | + private static final String MODULE_KEY = "FileAdjustor"; |
| 38 | + |
| 39 | + private FileAdjustor fileAdjustor; |
| 40 | + |
| 41 | + private SensorContextTester context; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setup() { |
| 45 | + context = SensorContextTester.create(PROJECT_DIR); |
| 46 | + addInputFiles("main.rs", "subfolder/main.rs"); |
| 47 | + fileAdjustor = FileAdjustor.create(context); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void should_return_relative_path_when_all_files_are_known() { |
| 52 | + assertThat(fileAdjustor.relativePath(path("foo", "bar", "FileAdjustor", "main.rs"))).isEqualTo("main.rs"); |
| 53 | + assertThat(fileAdjustor.relativePath(path("foo", "bar", "FileAdjustor", "subfolder", "main.rs"))).isEqualTo(path("subfolder", "main.rs")); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void should_return_relative_path_when_first_file_is_in_subfolder() { |
| 58 | + assertThat(fileAdjustor.relativePath(path("foo", "bar", "FileAdjustor", "subfolder", "main.rs"))).isEqualTo(path("subfolder", "main.rs")); |
| 59 | + assertThat(fileAdjustor.relativePath(path("foo", "bar", "FileAdjustor", "main.rs"))).isEqualTo("main.rs"); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + @Test |
| 64 | + public void should_not_return_relative_when_files_are_unknown() { |
| 65 | + assertThat(fileAdjustor.relativePath(path("foo", "bar", "FileAdjustor", "unknown.rs"))).isEqualTo(path("foo", "bar", "FileAdjustor", "unknown.rs")); |
| 66 | + assertThat(fileAdjustor.relativePath(path("foo", "bar", "FileAdjustor", "subfolder", "unknown.rs"))).isEqualTo(path("foo", "bar", "FileAdjustor", "subfolder", "unknown.rs")); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void should_return_relative_when_first_file_is_unknown() { |
| 71 | + assertThat(fileAdjustor.relativePath(path("foo", "bar", "FileAdjustor", "unknown.rs"))).isEqualTo(path("foo", "bar", "FileAdjustor", "unknown.rs")); |
| 72 | + assertThat(fileAdjustor.relativePath(path("foo", "bar", "FileAdjustor", "subfolder", "main.rs"))).isEqualTo(path("subfolder", "main.rs")); |
| 73 | + assertThat(fileAdjustor.relativePath(path("foo", "bar", "FileAdjustor", "main.rs"))).isEqualTo("main.rs"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void should_return_relative_path_when_already_relative() { |
| 78 | + assertThat(fileAdjustor.relativePath("main.rs")).isEqualTo("main.rs"); |
| 79 | + assertThat(fileAdjustor.relativePath(path("subfolder", "main.rs"))).isEqualTo(path("subfolder", "main.rs")); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void test() { |
| 84 | + assertThat(fileAdjustor.relativePath(path("unknown", "main.rs"))).isEqualTo(path("main.rs")); |
| 85 | + assertThat(fileAdjustor.relativePath("main.rs")).isEqualTo("main.rs"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void should_return_relative_path_for_second_file_when_unknown() { |
| 90 | + assertThat(fileAdjustor.relativePath(path("foo", "bar", "FileHandler", "main.rs"))).isEqualTo("main.rs"); |
| 91 | + assertThat(fileAdjustor.relativePath(path("foo", "bar", "FileHandler", "subfolder", "unknown.rs"))).isEqualTo(path("foo", "bar", "FileHandler", "subfolder", "unknown.rs")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void should_return_relative_path_for_unix_path() { |
| 96 | + assertThat(fileAdjustor.relativePath("/foo/bar/FileAdjustor/main.rs")).isEqualTo("main.rs"); |
| 97 | + assertThat(fileAdjustor.relativePath("/foo/bar/FileAdjustor/subfolder/main.rs")).isEqualTo(path("subfolder", "main.rs")); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void should_return_relative_path_for_fqn_windows_path() { |
| 102 | + assertThat(fileAdjustor.relativePath("C:\\foo\\bar\\FileAdjustor\\main.rs")).isEqualTo("main.rs"); |
| 103 | + assertThat(fileAdjustor.relativePath("C:\\foo\\bar\\FileAdjustor\\subfolder\\main.rs")).isEqualTo(path("subfolder", "main.rs")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void should_return_relative_path_for_relative_windows_path() { |
| 108 | + assertThat(fileAdjustor.relativePath("main.rs")).isEqualTo("main.rs"); |
| 109 | + assertThat(fileAdjustor.relativePath("subfolder\\main.rs")).isEqualTo("subfolder\\main.rs"); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void should_return_handle_shorter_path() { |
| 114 | + assertThat(fileAdjustor.relativePath(path("foo", "bar", "FileAdjustor", "main.rs"))).isEqualTo("main.rs"); |
| 115 | + assertThat(fileAdjustor.relativePath(path("bar", "main.rs"))).isEqualTo(path("bar","main.rs")); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + private void addInputFiles(String... paths) { |
| 120 | + Arrays.stream(paths).forEach(path -> context.fileSystem().add(TestInputFileBuilder.create(MODULE_KEY, path).build())); |
| 121 | + } |
| 122 | + |
| 123 | + private static String path(String first, String... more) { |
| 124 | + return Path.of(first, more).toString(); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | +} |
0 commit comments