|
| 1 | +/* |
| 2 | + * SonarC# |
| 3 | + * Copyright (C) 2014-2022 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.plugins.csharp; |
| 21 | + |
| 22 | +import org.junit.Rule; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.rules.TemporaryFolder; |
| 25 | +import org.sonar.api.batch.fs.InputFile; |
| 26 | +import org.sonar.api.batch.fs.internal.TestInputFileBuilder; |
| 27 | +import org.sonar.api.batch.sensor.cache.WriteCache; |
| 28 | +import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; |
| 29 | +import org.sonar.api.batch.sensor.internal.SensorContextTester; |
| 30 | +import org.sonar.api.config.internal.MapSettings; |
| 31 | +import org.sonar.api.utils.log.LogTester; |
| 32 | +import org.sonar.api.utils.log.LoggerLevel; |
| 33 | +import org.sonarsource.dotnet.shared.plugins.HashProvider; |
| 34 | + |
| 35 | +import java.io.File; |
| 36 | +import java.io.IOException; |
| 37 | +import java.security.NoSuchAlgorithmException; |
| 38 | + |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | +import static org.mockito.ArgumentMatchers.any; |
| 41 | +import static org.mockito.Mockito.mock; |
| 42 | +import static org.mockito.Mockito.when; |
| 43 | + |
| 44 | +public class CSharpFileCacheSensorTest { |
| 45 | + @Rule |
| 46 | + public TemporaryFolder temp = new TemporaryFolder(); |
| 47 | + |
| 48 | + @Rule |
| 49 | + public LogTester logTester = new LogTester(); |
| 50 | + |
| 51 | + @Test |
| 52 | + public void execute_whenCacheIsEnabled_itAddsOnlyTheLanguageFiles() throws IOException, NoSuchAlgorithmException { |
| 53 | + var basePath = temp.newFolder(); |
| 54 | + var settings = new MapSettings(); |
| 55 | + settings.setProperty(CSharpPlugin.FILE_SUFFIXES_KEY, ".cs"); |
| 56 | + settings.setProperty("sonar.pullrequest.cache.basepath", basePath.getCanonicalPath()); |
| 57 | + var hashProvider = mock(HashProvider.class); |
| 58 | + when(hashProvider.computeHash(any())).thenReturn(new byte[] {42} ); |
| 59 | + var context = SensorContextTester.create(basePath); |
| 60 | + context.setCacheEnabled(true); |
| 61 | + context.setSettings(settings); |
| 62 | + context.setNextCache(mock(WriteCache.class)); |
| 63 | + AddFile(context, basePath, "CSharp/Foo.cs", CSharpPlugin.LANGUAGE_KEY); |
| 64 | + AddFile(context, basePath, "VB/Bar.vb", "other-language-key"); |
| 65 | + var sut = new CSharpFileCacheSensor(new CSharp(settings.asConfig()), hashProvider); |
| 66 | + |
| 67 | + sut.execute(context); |
| 68 | + |
| 69 | + assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty(); |
| 70 | + assertThat(logTester.logs(LoggerLevel.DEBUG)).containsExactly( |
| 71 | + "Incremental PR analysis: Preparing to upload file hashes.", |
| 72 | + "Incremental PR analysis: Adding hash for 'CSharp/Foo.cs' to the cache." |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + private static void AddFile(SensorContextTester context, File basePath, String filePath, String languageKey) { |
| 77 | + context.fileSystem().add(new TestInputFileBuilder("project-key", basePath, new File(basePath, filePath)).setLanguage(languageKey).setType(InputFile.Type.MAIN).build()); |
| 78 | + } |
| 79 | +} |
0 commit comments