Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RepositoryToBeCreatedFixtureBase.cs « libgit2sharp.Tests - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0d1bfe2d4dcf70ccf6bfef25a417d5a4d8949516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.IO;
using System.Reflection;
using NUnit.Framework;

namespace libgit2sharp.Tests
{
    public class RepositoryToBeCreatedFixtureBase
    {
        private const string TestRepositoriesDirectoryName = "TestRepos";
        private static readonly string _testRepositoriesDirectoryPath = RetrieveTestRepositoriesDirectory();
        private string _pathToTempDirectory;

        protected string PathToTempDirectory { get { return _pathToTempDirectory; } }

        [SetUp]
        public virtual void Setup()
        {
            string workDirpath = Path.Combine(_testRepositoriesDirectoryPath, this.GetType().Name, Guid.NewGuid().ToString().Substring(0, 8));

            Directory.CreateDirectory(workDirpath);

            _pathToTempDirectory = workDirpath;
        }

        [TestFixtureTearDown]
        public virtual void TestFixtureTearDown()
        {
            DeleteDirectory(_testRepositoriesDirectoryPath);
        }

        private static void DeleteDirectory(string directoryPath)
        {
            // From http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/329502#329502

            string[] files = Directory.GetFiles(directoryPath);
            string[] dirs = Directory.GetDirectories(directoryPath);

            foreach (string file in files)
            {
                File.SetAttributes(file, FileAttributes.Normal);
                File.Delete(file);
            }

            foreach (string dir in dirs)
            {
                DeleteDirectory(dir);
            }

            File.SetAttributes(directoryPath, FileAttributes.Normal);
            Directory.Delete(directoryPath, false);
        }

        static private string RetrieveAssemblyDirectory()
        {
            // From http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in/283917#283917

            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            var uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            return Path.GetDirectoryName(path);
        }

        static private string RetrieveTestRepositoriesDirectory()
        {
            return Path.Combine(RetrieveAssemblyDirectory(), TestRepositoriesDirectoryName);
        }
    }
}