Skip to content

Commit

Permalink
Review 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-paidis-sonarsource committed Sep 9, 2024
1 parent 4e02fd8 commit 5ada95e
Show file tree
Hide file tree
Showing 8 changed files with 466 additions and 569 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ public void GetProjectGuidAsString_WhenProjectInfoIsNull_ThrowsArgumentNullExcep
public void GetAllAnalysisFilesTest()
{
var dir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext);
var filesToAnalyze = Path.Combine(dir, "FilesToAnalyze.txt");
var filesToAnalyze = Path.Combine(dir, TestUtils.FilesToAnalyze);
var logger = new TestLogger();
var projectInfo = new ProjectInfo
{
AnalysisResults =
[
new AnalysisResult
{
Id = "FilesToAnalyze",
Id = TestUtils.FilesToAnalyze,
Location = filesToAnalyze,
}
]
Expand All @@ -163,14 +163,16 @@ public void GetAllAnalysisFilesTest()
[
"C:\\foo",
"C:\\bar",
"not:allowed"
"not:allowed",
"C:\\baz",
]);

var result = projectInfo.GetAllAnalysisFiles(logger);

result.Should().HaveCount(2);
result.Should().HaveCount(3);
result[0].Name.Should().Be("foo");
result[1].Name.Should().Be("bar");
logger.AssertSingleDebugMessageExists("File could not be added to analysis files: 'not:allowed'.");
result[2].Name.Should().Be("baz");
logger.AssertSingleDebugMessageExists("Could not add 'not:allowed' to the analysis. The given path's format is not supported.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ public void GenerateFile_MissingFilesAreSkipped()
Encoding = "UTF-8"
};

var analysisFileList = CreateFileList(projectBaseDir, "filesToAnalyze.txt", existingManagedFile, missingManagedFile, existingContentFile, missingContentFile);
var analysisFileList = CreateFileList(projectBaseDir, TestUtils.FilesToAnalyze, existingManagedFile, missingManagedFile, existingContentFile, missingContentFile);
projectInfo.AddAnalyzerResult(AnalysisType.FilesToAnalyze, analysisFileList);
var projectInfoDir = TestUtils.CreateTestSpecificFolderWithSubPaths(TestContext, "ProjectInfo1Dir");
var projectInfoFilePath = Path.Combine(projectInfoDir, FileConstants.ProjectInfoFileName);
Expand Down Expand Up @@ -849,7 +849,7 @@ public void TryWriteProperties_WhenThereIsNoCommonPath_LogsError()
var outPath = Path.Combine(TestContext.TestRunDirectory!, ".sonarqube", "out");
Directory.CreateDirectory(outPath);
var fileToAnalyzePath = TestUtils.CreateEmptyFile(TestContext.TestRunDirectory, "file.cs");
var filesToAnalyzePath = TestUtils.CreateFile(TestContext.TestRunDirectory, "FilesToAnalyze.txt", fileToAnalyzePath);
var filesToAnalyzePath = TestUtils.CreateFile(TestContext.TestRunDirectory, TestUtils.FilesToAnalyze, fileToAnalyzePath);
var config = new AnalysisConfig { SonarOutputDir = outPath };
var sut = new PropertiesFileGenerator(config, logger);

Expand All @@ -859,15 +859,15 @@ public void TryWriteProperties_WhenThereIsNoCommonPath_LogsError()
FullPath = Path.Combine(TestContext.TestRunDirectory, "First"),
ProjectName = "First",
AnalysisSettings = [],
AnalysisResults = [new AnalysisResult { Id = "FilesToAnalyze", Location = filesToAnalyzePath }]
AnalysisResults = [new AnalysisResult { Id = TestUtils.FilesToAnalyze, Location = filesToAnalyzePath }]
};
var secondProjectInfo = new ProjectInfo
{
ProjectGuid = Guid.NewGuid(),
FullPath = Path.Combine(Path.GetTempPath(), "Second"),
ProjectName = "Second",
AnalysisSettings = [],
AnalysisResults = [new AnalysisResult { Id = "FilesToAnalyze", Location = filesToAnalyzePath }]
AnalysisResults = [new AnalysisResult { Id = TestUtils.FilesToAnalyze, Location = filesToAnalyzePath }]
};
TestUtils.CreateEmptyFile(TestContext.TestRunDirectory, "First");
TestUtils.CreateEmptyFile(Path.GetTempPath(), "Second");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private static void CreateProjectInfoAndFilesToAnalyze(Guid guid,

// Create FilesToAnalyze.txt in each folder, they are the same,
// because are the result of the compilation of the same project
var filesToAnalyze_txt = Path.Combine(@out, "FilesToAnalyze.txt");
var filesToAnalyze_txt = Path.Combine(@out, TestUtils.FilesToAnalyze);
File.WriteAllLines(filesToAnalyze_txt, files.ToArray());

// Create project info for the configuration, the project path is important, the name is ignored
Expand All @@ -280,7 +280,7 @@ private static void CreateProjectInfoAndFilesToAnalyze(Guid guid,
ProjectType = ProjectType.Product,
Encoding = "UTF-8",
IsExcluded = isExcluded,
AnalysisResults = [new() { Id = "FilesToAnalyze", Location = filesToAnalyze_txt }]
AnalysisResults = [new() { Id = TestUtils.FilesToAnalyze, Location = filesToAnalyze_txt }]
};
TestUtils.CreateEmptyFile(projectRoot, "Project1.csproj");
projectInfo.Save(Path.Combine(@out, FileConstants.ProjectInfoFileName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public void E2E_NoAnalyzableFiles()
actualStructure.AssertConfigFileDoesNotExist(ExpectedAnalysisFilesListFileName);

// Check the projectInfo.xml does not have an analysis result
actualStructure.ProjectInfo.AssertAnalysisResultDoesNotExists("FilesToAnalyze");
actualStructure.ProjectInfo.AssertAnalysisResultDoesNotExists(TestUtils.FilesToAnalyze);
}

[TestMethod]
Expand Down Expand Up @@ -959,7 +959,7 @@ private static void AssertProjectInfoContent(ProjectInfo projectInfo, string exp
{
projectInfo.ProjectLanguage.Should().Be("my.language", "Unexpected project language");
projectInfo.ProjectType.Should().Be(ProjectType.Product, "Project should be marked as a product project");
projectInfo.AnalysisResults.Single(x => x.Id.Equals("FilesToAnalyze")).Location.Should().Be(expectedFilesToAnalyzePath);
projectInfo.AnalysisResults.Single(x => x.Id.Equals(TestUtils.FilesToAnalyze)).Location.Should().Be(expectedFilesToAnalyzePath);
projectInfo.AnalysisSettings.Single(x => x.Id.Equals("sonar.cs.roslyn.reportFilePaths")).Value.Should().Be(expectedReportFilePaths);
projectInfo.AnalysisSettings.Single(x => x.Id.Equals("sonar.cs.analyzer.projectOutPaths")).Value.Should().Be(expectedProjectOutPaths);
}
Expand Down Expand Up @@ -1121,7 +1121,7 @@ public void AssertExpectedFileList(params string[] fileNames)
var expectedFullPaths = fileNames.Select(x => context.InputFolder + x);
File.ReadLines(filesToAnalyzeFile.FullPath).Should().BeEquivalentTo(expectedFullPaths);

var actualFilesToAnalyze = ProjectInfo.AssertAnalysisResultExists("FilesToAnalyze");
var actualFilesToAnalyze = ProjectInfo.AssertAnalysisResultExists(TestUtils.FilesToAnalyze);
actualFilesToAnalyze.Location.Should().Be(filesToAnalyzeFile.FullPath);

AssertFileIsUtf8Bom(filesToAnalyzeFile.FullPath);
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestUtilities/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ namespace TestUtilities;

public static class TestUtils
{
public const string FilesToAnalyze = nameof(FilesToAnalyze);
// Target file names
public const string AnalysisTargetFile = "SonarQube.Integration.targets";

public const string ImportsBeforeFile = "SonarQube.Integration.ImportBefore.targets";

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ public static FileInfo[] GetAllAnalysisFiles(this ProjectInfo projectInfo, ILogg
{
result.Add(new FileInfo(path));
}
catch (Exception)
catch (Exception ex)
{
logger.LogDebug(Resources.MSG_AnalysisFileCouldNotBeAdded, path);
logger.LogDebug(Resources.MSG_AnalysisFileCouldNotBeAdded, path, ex.Message);
}
}
return [.. result];
Expand Down
Loading

0 comments on commit 5ada95e

Please sign in to comment.