Skip to content

Commit 5480c4d

Browse files
Fix relative CLI paths for config and file options
Resolve config paths with Path.GetFullPath before SetBasePath, and normalize baseline, output, and coverage paths in AnalysisWorkflow.Prepare. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 4045f2b commit 5480c4d

4 files changed

Lines changed: 69 additions & 9 deletions

File tree

CognitiveCodeAnalysis.Tests/src/Application/AnalysisWorkflowTests.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void Prepare_ResolvesPathsAndDefaults()
3030

3131
Assert.That(prepared.AbsoluteSourcePath, Is.EqualTo(Path.GetFullPath(".")));
3232
Assert.That(prepared.ReportType, Is.EqualTo("Html"));
33-
Assert.That(prepared.OutputFile, Is.EqualTo("cognitive-analysis-report"));
33+
Assert.That(prepared.OutputFile, Is.EqualTo(Path.GetFullPath("cognitive-analysis-report")));
3434
Assert.That(prepared.IsConsoleTextReport, Is.False);
3535
}
3636

@@ -49,7 +49,28 @@ public void Prepare_RecognizesConsoleTextReport()
4949
));
5050

5151
Assert.That(prepared.IsConsoleTextReport, Is.True);
52-
Assert.That(prepared.OutputFile, Is.EqualTo("out.txt"));
52+
Assert.That(prepared.OutputFile, Is.EqualTo(Path.GetFullPath("out.txt")));
53+
}
54+
55+
[Test]
56+
public void Prepare_ResolvesRelativeBaselineAndOutputPaths()
57+
{
58+
var workflow = CreateWorkflow();
59+
var baseline = Path.Combine(".", "baseline.json");
60+
var output = Path.Combine(".", "report.html");
61+
62+
var prepared = workflow.Prepare(new AnalysisRequest(
63+
SourcePath: ".",
64+
ConfigFile: null,
65+
ReportType: "Html",
66+
BaselineFile: baseline,
67+
OutputFile: output,
68+
CoverageCobertura: Path.Combine(".", "coverage.xml")
69+
));
70+
71+
Assert.That(prepared.BaselineFile, Is.EqualTo(Path.GetFullPath(baseline)));
72+
Assert.That(prepared.OutputFile, Is.EqualTo(Path.GetFullPath(output)));
73+
Assert.That(prepared.CoverageCobertura, Is.EqualTo(Path.GetFullPath(Path.Combine(".", "coverage.xml"))));
5374
}
5475

5576
[Test]

CognitiveCodeAnalysis.Tests/src/Configuration/ConfigurationLoaderTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,42 @@ public void ConfigureServices_GetConfiguration_RoundTrips()
9898
}
9999
}
100100

101+
[Test]
102+
public void Load_RelativeConfigPath_ResolvesAgainstCurrentDirectory()
103+
{
104+
var json = """
105+
{
106+
"cognitive": {
107+
"scoreThreshold": 3.21
108+
}
109+
}
110+
""";
111+
var dir = Path.Combine(Path.GetTempPath(), "cogcfg-rel-" + Guid.NewGuid());
112+
var subDir = Path.Combine(dir, "config");
113+
Directory.CreateDirectory(subDir);
114+
var file = Path.Combine(subDir, "custom.json");
115+
var originalCwd = Directory.GetCurrentDirectory();
116+
try
117+
{
118+
File.WriteAllText(file, json);
119+
Directory.SetCurrentDirectory(dir);
120+
var cfg = ConfigurationLoader.Load(Path.Combine("config", "custom.json"));
121+
Assert.That(cfg.ScoreThreshold, Is.EqualTo(3.21));
122+
}
123+
finally
124+
{
125+
Directory.SetCurrentDirectory(originalCwd);
126+
try
127+
{
128+
Directory.Delete(dir, recursive: true);
129+
}
130+
catch
131+
{
132+
// ignore
133+
}
134+
}
135+
}
136+
101137
[Test]
102138
public void Load_WithoutConfigFile_UsesBundledDefaults()
103139
{

CognitiveCodeAnalysis/src/Application/AnalysisWorkflow.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <copyright company="Florian Krämer">
1+
/// <copyright company="Florian Krämer">
22
/// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
/// </copyright>
44

@@ -20,20 +20,23 @@ public PreparedAnalysis Prepare(AnalysisRequest request)
2020
var sourcePath = request.SourcePath ?? Directory.GetCurrentDirectory();
2121
var absoluteSourcePath = Path.GetFullPath(sourcePath);
2222
var reportType = request.ReportType;
23-
var outputFile = request.OutputFile ?? "cognitive-analysis-report";
23+
var outputFile = Path.GetFullPath(request.OutputFile ?? "cognitive-analysis-report");
2424
var isConsoleTextReport = string.Equals(reportType, "ConsoleText", StringComparison.OrdinalIgnoreCase);
2525

2626
return new PreparedAnalysis(
2727
Configuration: configuration,
2828
AbsoluteSourcePath: absoluteSourcePath,
2929
ReportType: reportType,
3030
OutputFile: outputFile,
31-
BaselineFile: request.BaselineFile,
32-
CoverageCobertura: request.CoverageCobertura,
31+
BaselineFile: ToAbsolutePath(request.BaselineFile),
32+
CoverageCobertura: ToAbsolutePath(request.CoverageCobertura),
3333
IsConsoleTextReport: isConsoleTextReport
3434
);
3535
}
3636

37+
private static string? ToAbsolutePath(string? path) =>
38+
string.IsNullOrWhiteSpace(path) ? path : Path.GetFullPath(path);
39+
3740
public List<string> FindSourceFiles(string absoluteSourcePath, IProgress<AnalysisProgress>? progress = null)
3841
=> cognitiveAnalysisFacade.FindSourceFiles(absoluteSourcePath, progress);
3942

CognitiveCodeAnalysis/src/Configuration/ConfigurationLoader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ private static IConfigurationRoot BuildConfiguration(string? configFilePath = nu
6666

6767
if (string.IsNullOrEmpty(configFilePath)) return GetDefaultConfig();
6868

69-
// If a specific file path is provided, use it directly
70-
string directory = Path.GetDirectoryName(configFilePath) ?? AppContext.BaseDirectory;
71-
string fileName = Path.GetFileName(configFilePath);
69+
string fullConfigPath = Path.GetFullPath(configFilePath);
70+
string directory = Path.GetDirectoryName(fullConfigPath) ?? AppContext.BaseDirectory;
71+
string fileName = Path.GetFileName(fullConfigPath);
7272

7373
return builder.SetBasePath(directory)
7474
.AddJsonFile(fileName, optional: false, reloadOnChange: false)

0 commit comments

Comments
 (0)