Skip to content

Commit fba0cca

Browse files
CopilotJanProvaznik
andcommitted
Add test for relative OutputDirectory producing relative OutputFile
Co-authored-by: JanProvaznik <25267098+JanProvaznik@users.noreply.github.com>
1 parent af34a81 commit fba0cca

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

src/Tasks.UnitTests/WriteCodeFragment_Tests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,55 @@ public void ToDirectoryAndDirectoryDoesNotExist()
421421
Assert.Equal(".cs", task.OutputFile.ItemSpec.Substring(task.OutputFile.ItemSpec.Length - 3));
422422
}
423423

424+
/// <summary>
425+
/// When OutputDirectory is relative and OutputFile is not specified, the resulting OutputFile should be relative.
426+
/// </summary>
427+
[Fact]
428+
public void RelativeOutputDirectoryProducesRelativeOutputFile()
429+
{
430+
using TestEnvironment env = TestEnvironment.Create();
431+
432+
// Create an actual folder and get a relative path to it
433+
string absoluteFolder = env.CreateFolder().Path;
434+
string relativeFolder = Path.GetFileName(absoluteFolder);
435+
436+
// Change current directory to the parent so the relative path works
437+
string originalDir = Directory.GetCurrentDirectory();
438+
try
439+
{
440+
Directory.SetCurrentDirectory(Path.GetDirectoryName(absoluteFolder));
441+
442+
WriteCodeFragment task = new WriteCodeFragment();
443+
task.TaskEnvironment = TaskEnvironmentHelper.CreateForTest();
444+
MockEngine engine = new MockEngine(true);
445+
task.BuildEngine = engine;
446+
TaskItem attribute = new TaskItem("System.AssemblyTrademarkAttribute");
447+
task.AssemblyAttributes = new TaskItem[] { attribute };
448+
task.Language = "c#";
449+
task.OutputDirectory = new TaskItem(relativeFolder);
450+
bool result = task.Execute();
451+
452+
result.ShouldBeTrue(engine.Log);
453+
454+
// The output file should be relative (not rooted) since OutputDirectory was relative
455+
Path.IsPathRooted(task.OutputFile.ItemSpec).ShouldBeFalse("OutputFile should be relative when OutputDirectory is relative");
456+
457+
// The output file should start with the relative folder name
458+
task.OutputFile.ItemSpec.ShouldStartWith(relativeFolder);
459+
460+
// Cleanup the generated file
461+
string absoluteOutputFile = Path.Combine(Path.GetDirectoryName(absoluteFolder), task.OutputFile.ItemSpec);
462+
if (File.Exists(absoluteOutputFile))
463+
{
464+
File.Delete(absoluteOutputFile);
465+
}
466+
}
467+
finally
468+
{
469+
Directory.SetCurrentDirectory(originalDir);
470+
}
471+
}
472+
424473
/// <summary>
425474
/// Regular case
426475
/// </summary>

0 commit comments

Comments
 (0)