﻿using System;
using System.IO;
using Ionic.Zip;
using NUnit.Framework;

namespace MSBuild.Version.Tasks.Tests
{
    [TestFixture]
    public class GitVersionFileTest
    {
        [SetUp]
        public void SetUp()
        {
            // extract the repositories in the 'Git.zip' file into the 'temp' folder
            using (ZipFile zipFile = ZipFile.Read(Path.Combine(RepositoriesDirectory, "Git.zip")))
            {
                foreach (ZipEntry zipEntry in zipFile)
                {
                    zipEntry.Extract(TempDirectory, ExtractExistingFileAction.OverwriteSilently);
                }
            }
        }

        [TearDown]
        public void TearDown()
        {
            // delete the 'temp' folder
            Directory.Delete(TempDirectory, true);
        }

        [Test]
        public void Execute_WriteRevision_ShouldWriteRevision()
        {
            Execute("Revision.tmp", "Git1Revision.txt", "git1");
            Assert.AreEqual(ReadFirstLine("Git1Revision.txt"), "2");

            Execute("Revision.tmp", "Git2Revision.txt", "git2");
            Assert.AreEqual(ReadFirstLine("Git2Revision.txt"), "4");

            Execute("Revision.tmp", "Git3Revision.txt", "git3");
            Assert.AreEqual(ReadFirstLine("Git3Revision.txt"), "2");
        }

        private static readonly string TemplatesDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data\\Templates");
        private static readonly string RepositoriesDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data\\Repositories");
        private static readonly string TempDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data\\Temp");

        private static void Execute(string templateFile, string destinationFile, string workingDirectory)
        {
            GitVersionFile gitVersionFile = new GitVersionFile
            {
                TemplateFile = Path.Combine(TemplatesDirectory, templateFile),
                DestinationFile = Path.Combine(TempDirectory, destinationFile),
                WorkingDirectory = Path.Combine(TempDirectory, workingDirectory)
            };

            gitVersionFile.Execute();
        }

        private static string ReadFirstLine(string fileName)
        {
            using (StreamReader streamReader = new StreamReader(Path.Combine(TempDirectory, fileName)))
            {
                return streamReader.ReadLine();
            }
        }
    }
}

Add first attempt of the Git unit tests
Add first attempt of the Git unit tests
