|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.IO; |
| 5 | +using LibGit2Sharp; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | + |
| 8 | +namespace GmodNET.VersionTool.Core.Tests.Helpers |
| 9 | +{ |
| 10 | + public class TempVersionFileProvider : IDisposable |
| 11 | + { |
| 12 | + string file_path; |
| 13 | + bool was_disposed; |
| 14 | + |
| 15 | + public string FilePath => this.file_path; |
| 16 | + |
| 17 | + public TempVersionFileProvider() |
| 18 | + { |
| 19 | + file_path = Path.GetTempPath() + Guid.NewGuid().ToString() + ".json"; |
| 20 | + was_disposed = false; |
| 21 | + } |
| 22 | + |
| 23 | + ~TempVersionFileProvider() |
| 24 | + { |
| 25 | + Dispose(); |
| 26 | + } |
| 27 | + |
| 28 | + public void Dispose() |
| 29 | + { |
| 30 | + if (!was_disposed) |
| 31 | + { |
| 32 | + if (File.Exists(file_path)) |
| 33 | + { |
| 34 | + File.Delete(file_path); |
| 35 | + } |
| 36 | + was_disposed = true; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public class TempRepoProvider : IDisposable |
| 42 | + { |
| 43 | + bool wasDisposed; |
| 44 | + DirectoryInfo repoDirectory; |
| 45 | + string version_file_path; |
| 46 | + |
| 47 | + public DirectoryInfo RepoDirectory => repoDirectory; |
| 48 | + |
| 49 | + public string RepoVersionFilePath => version_file_path; |
| 50 | + |
| 51 | + public TempRepoProvider(string version_file_to_copy_into_repo, DateTimeOffset time_of_the_initial_commit, bool make_initial_commit = true) |
| 52 | + { |
| 53 | + wasDisposed = false; |
| 54 | + |
| 55 | + repoDirectory = Directory.CreateDirectory(Path.GetTempPath() + Guid.NewGuid().ToString()); |
| 56 | + |
| 57 | + Repository.Init(repoDirectory.FullName); |
| 58 | + |
| 59 | + version_file_path = Path.Combine(repoDirectory.FullName, version_file_to_copy_into_repo); |
| 60 | + |
| 61 | + File.Copy(version_file_to_copy_into_repo, version_file_path); |
| 62 | + |
| 63 | + using Repository repository = new Repository(repoDirectory.FullName); |
| 64 | + |
| 65 | + if (make_initial_commit) |
| 66 | + { |
| 67 | + Commands.Stage(repository, "*"); |
| 68 | + |
| 69 | + Signature repo_commiter_signature = new Signature("Test runner", "support@gmodnet.xyz", time_of_the_initial_commit); |
| 70 | + |
| 71 | + repository.Commit("Initial commit", repo_commiter_signature, repo_commiter_signature); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public TempRepoProvider(string version_file_to_copy_into_repo) : this(version_file_to_copy_into_repo, DateTimeOffset.Now, true) |
| 76 | + { |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + public void Dispose() |
| 81 | + { |
| 82 | + if(!wasDisposed) |
| 83 | + { |
| 84 | + if(repoDirectory.Exists) |
| 85 | + { |
| 86 | + repoDirectory.SetFilesAttributesToNormal(); |
| 87 | + repoDirectory.Delete(true); |
| 88 | + } |
| 89 | + wasDisposed = true; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + ~TempRepoProvider() |
| 94 | + { |
| 95 | + Dispose(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static class HelperExtensions |
| 100 | + { |
| 101 | + public static void SetFilesAttributesToNormal(this DirectoryInfo directory) |
| 102 | + { |
| 103 | + foreach (FileInfo file in directory.GetFiles()) |
| 104 | + { |
| 105 | + File.SetAttributes(file.FullName, FileAttributes.Normal); |
| 106 | + } |
| 107 | + |
| 108 | + foreach (DirectoryInfo subdirectory in directory.GetDirectories()) |
| 109 | + { |
| 110 | + subdirectory.SetFilesAttributesToNormal(); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments