Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FakeFileSystem throws on linux, when it is created on a Windows FakeEnvironment #4322

Open
2 tasks done
nils-a opened this issue Aug 1, 2024 · 1 comment · May be fixed by #4323
Open
2 tasks done

FakeFileSystem throws on linux, when it is created on a Windows FakeEnvironment #4322

nils-a opened this issue Aug 1, 2024 · 1 comment · May be fixed by #4323

Comments

@nils-a
Copy link
Member

nils-a commented Aug 1, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched issues to ensure it has not already been reported

Cake runner

Cake .NET Tool

Cake version

4.0.0

Operating system

Linux

Operating system architecture

64-Bit

CI Server

No response

What are you seeing?

demo code

using Cake.Core;
using Cake.Core.IO;
using Cake.Testing;
using Xunit;

namespace test1;

public class Tests
{
    [Fact]
    public void T1()
    {
        var fixture = new MyFixture();
        
        Assert.True(fixture.FileSystem.GetDirectory(fixture.Environment.WorkingDirectory).Exists);
    }
}

public class MyFixture
{
    public ICakeEnvironment Environment { get; }
    public IFileSystem FileSystem { get; }

    public MyFixture()
    {
        Environment = FakeEnvironment.CreateWindowsEnvironment();
        FileSystem = new FakeFileSystem(Environment);
    }
}

error

System.ArgumentException: Working directory cannot be relative.

System.ArgumentException
Working directory cannot be relative.
   at Cake.Testing.FakeFileSystemTree..ctor(ICakeEnvironment environment)
   at Cake.Testing.FakeFileSystem..ctor(ICakeEnvironment environment)
   at test1.MyFixture..ctor() in /home/nils/dev/test1/Class1.cs:line 27
   at test1.Tests.T1() in /home/nils/dev/test1/Class1.cs:line 13
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

What is expected?

I expect no error to occur.

Steps to Reproduce

  • Create a new project
  • Add references
    • Cake.Testing
    • Microsoft.NET.Test.Sdk
    • xunit
  • Use the code from above
  • run the test

Output log

No response

@nils-a
Copy link
Member Author

nils-a commented Aug 1, 2024

Cause of the problem:

When the FakeFileSystem is created, it creates a FakeFileSystemTree using the given ICakeEnvironment:

_tree = new FakeFileSystemTree(environment);

The FakeFileSystemTree checks whether environment.WorkingDirectory.IsRelative is true and if so, it throws the exception:

if (environment.WorkingDirectory.IsRelative)
{
throw new ArgumentException("Working directory cannot be relative.");
}

Now, ICakeEnvironment.WorkingDirectory.IsRelative is defined as:

IsRelative = !PathHelper.IsPathRooted(FullPath);

PathHelper.IsPathRooted relies on

private static readonly bool _isWindows = EnvironmentHelper.GetPlatformFamily() == PlatformFamily.Windows;

And EnvironmentHelper.GetPlatformFamily in turn checks, what the current system is. I.e. the system running the test, not the FakeEnvironment that was defined in the test.

Thus, the WorkingDirectory, which is defined in FakeEnvironment as

environment.WorkingDirectory = new DirectoryPath("C:/Working");

is checked for IsRooted on my linux system, and not on the FakeEnvironment.

On my linux system, C:/Working is not rooted. Thus, it is treated as relative, and the exception is thrown.

nils-a added a commit to nils-a/cake-build--cake that referenced this issue Aug 3, 2024
@nils-a nils-a linked a pull request Aug 3, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant