0

I've got a C# access exception problem. Here is my integration test:

[TestFixture]
public class DirectoryTraverserTest
{
    private string testDirectory;

    [SetUp]
    public void Setup()
    {
        testDirectory = CreateTempDirectory();

        var files = new[] { "file1","file2","file3","file4"};
        CreateFiles(testDirectory, files);


        System.Console.WriteLine(testDirectory);
    }

    [TearDown]
    public void TearDown()
    {
        Directory.Delete(testDirectory, true);
    }

    [Test]
    public void testThatFilesComeBackInOrderFileOrder()
    {
        CreateFile(testDirectory, "order.mnl", "file4\nfile3\nfile2\nfile1\n");

        var result = new List<FileInfo>(new DirectoryTraverser(testDirectory));
        Assert.That(result[0].FullName, Is.EqualTo(Path.Combine(testDirectory, "file4")));
        Assert.That(result[1].FullName, Is.EqualTo(Path.Combine(testDirectory, "file3")));
        Assert.That(result[2].FullName, Is.EqualTo(Path.Combine(testDirectory, "file2")));
        Assert.That(result[3].FullName, Is.EqualTo(Path.Combine(testDirectory, "file1")));
    }

    [Test]
    public void testThatFilesComeBackInFileSystemOrder()
    {
        var result = new List<FileInfo>(new DirectoryTraverser(testDirectory));
        Assert.That(result[0].FullName, Is.EqualTo(Path.Combine(testDirectory, "file1")));
        Assert.That(result[1].FullName, Is.EqualTo(Path.Combine(testDirectory, "file2")));
        Assert.That(result[2].FullName, Is.EqualTo(Path.Combine(testDirectory, "file3")));
        Assert.That(result[3].FullName, Is.EqualTo(Path.Combine(testDirectory, "file4")));
    }

    public static string CreateTempDirectory()
    {
        var path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
        Directory.CreateDirectory(path);
        return path;
    }

    public static void CreateFiles(string path, string[] files)
    {
        foreach (var file in files)
            CreateFile(path, file, file);
    }

    public static void CreateFile(string path, string name, string content)
    {
        using(var f = new StreamWriter(Path.Combine(path, name)))
        {
            f.Write(content);
        }
    }
}

And here is the class under test (prior to the refactor step as the test does not pass):

public class DirectoryTraverser : IEnumerable<FileInfo>
{
    private const string ORDER_FILENAME = "order.mnl";
    private readonly IList<FileInfo> order;

    public DirectoryTraverser(string dirPath)
    {
        var orderfilePath = Path.Combine(dirPath, ORDER_FILENAME);
        if (new FileInfo(orderfilePath).Exists)
        {
            order = new List<FileInfo>();
            using (var sr = File.OpenText(dirPath))
            {
                string file;
                while ((file = sr.ReadLine()) != null)
                    order.Add(new FileInfo(Path.Combine(dirPath, file)));
            }
        }
        else
        {
            order = new List<FileInfo>(new DirectoryInfo(dirPath).GetFiles());
        }
    }

    public IEnumerator<FileInfo> GetEnumerator()
    {
        return order.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

The output from NUnit:

Test net.purpletube.minla.test.FileSystem.DirectoryTraverserTest.testThatFilesComeBackInOrderFileOrder'
failed: System.UnauthorizedAccessException : Access to the path 'C:...\Temp\tto2nvka.1es' is denied.
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) 
       at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
       at System.IO.StreamReader..ctor(String path)
       at System.IO.File.OpenText(String path)
       FileSystem\DirectoryTraverser.cs(21,0): at net.purpletube.minla.FileSystem.DirectoryTraverser..ctor(String
dirPath)
       FileSystem\DirectoryTraverserTest.cs(36,0): at net.purpletube.minla.test.FileSystem.DirectoryTraverserTest.testThatFilesComeBackInOrderFileOrder()

1 passed, 1 failed, 0 skipped, took 0.33 seconds (NUnit 2.5.5).

It appears that reading the four temporary files (file1 to file4) is fine. It's the order file that causes the problem.

I'm completely lost. Does anyone have a clue what might be happening?

1 Answer 1

2

It looks to me like you are trying to open a directory instead of a file:

File.OpenText(dirPath)

If dirPath is actually a path to a directory rather than to a file then this will fail.

1
  • Another reason to pass directories as DirectoryInfos rather than path strings. May 23, 2013 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.