|
| 1 | +using GVFS.CommandLine; |
| 2 | +using NUnit.Framework; |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | + |
| 6 | +namespace GVFS.UnitTests.CommandLine |
| 7 | +{ |
| 8 | + [TestFixture] |
| 9 | + public class CacheVerbTests |
| 10 | + { |
| 11 | + private CacheVerb cacheVerb; |
| 12 | + private string testDir; |
| 13 | + |
| 14 | + [SetUp] |
| 15 | + public void Setup() |
| 16 | + { |
| 17 | + this.cacheVerb = new CacheVerb(); |
| 18 | + this.testDir = Path.Combine(Path.GetTempPath(), "CacheVerbTests_" + Guid.NewGuid().ToString("N")); |
| 19 | + Directory.CreateDirectory(this.testDir); |
| 20 | + } |
| 21 | + |
| 22 | + [TearDown] |
| 23 | + public void TearDown() |
| 24 | + { |
| 25 | + if (Directory.Exists(this.testDir)) |
| 26 | + { |
| 27 | + Directory.Delete(this.testDir, recursive: true); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + [TestCase(0, "0 bytes")] |
| 32 | + [TestCase(512, "512 bytes")] |
| 33 | + [TestCase(1023, "1023 bytes")] |
| 34 | + [TestCase(1024, "1.0 KB")] |
| 35 | + [TestCase(1536, "1.5 KB")] |
| 36 | + [TestCase(1048576, "1.0 MB")] |
| 37 | + [TestCase(1572864, "1.5 MB")] |
| 38 | + [TestCase(1073741824, "1.0 GB")] |
| 39 | + [TestCase(1610612736, "1.5 GB")] |
| 40 | + [TestCase(10737418240, "10.0 GB")] |
| 41 | + public void FormatSizeReturnsExpectedString(long bytes, string expected) |
| 42 | + { |
| 43 | + Assert.AreEqual(expected, this.cacheVerb.FormatSize(bytes)); |
| 44 | + } |
| 45 | + |
| 46 | + [TestCase] |
| 47 | + public void GetPackSummaryWithNoPacks() |
| 48 | + { |
| 49 | + string packDir = Path.Combine(this.testDir, "pack"); |
| 50 | + Directory.CreateDirectory(packDir); |
| 51 | + |
| 52 | + this.cacheVerb.GetPackSummary( |
| 53 | + packDir, |
| 54 | + out int prefetchCount, |
| 55 | + out long prefetchSize, |
| 56 | + out int otherCount, |
| 57 | + out long otherSize, |
| 58 | + out long latestTimestamp); |
| 59 | + |
| 60 | + Assert.AreEqual(0, prefetchCount); |
| 61 | + Assert.AreEqual(0, prefetchSize); |
| 62 | + Assert.AreEqual(0, otherCount); |
| 63 | + Assert.AreEqual(0, otherSize); |
| 64 | + Assert.AreEqual(0, latestTimestamp); |
| 65 | + } |
| 66 | + |
| 67 | + [TestCase] |
| 68 | + public void GetPackSummaryCategorizesPrefetchAndOtherPacks() |
| 69 | + { |
| 70 | + string packDir = Path.Combine(this.testDir, "pack"); |
| 71 | + Directory.CreateDirectory(packDir); |
| 72 | + |
| 73 | + this.CreateFileWithSize(Path.Combine(packDir, "prefetch-1000-aabbccdd.pack"), 100); |
| 74 | + this.CreateFileWithSize(Path.Combine(packDir, "prefetch-2000-eeff0011.pack"), 200); |
| 75 | + this.CreateFileWithSize(Path.Combine(packDir, "pack-abcdef1234567890.pack"), 50); |
| 76 | + |
| 77 | + this.cacheVerb.GetPackSummary( |
| 78 | + packDir, |
| 79 | + out int prefetchCount, |
| 80 | + out long prefetchSize, |
| 81 | + out int otherCount, |
| 82 | + out long otherSize, |
| 83 | + out long latestTimestamp); |
| 84 | + |
| 85 | + Assert.AreEqual(2, prefetchCount); |
| 86 | + Assert.AreEqual(300, prefetchSize); |
| 87 | + Assert.AreEqual(1, otherCount); |
| 88 | + Assert.AreEqual(50, otherSize); |
| 89 | + Assert.AreEqual(2000, latestTimestamp); |
| 90 | + } |
| 91 | + |
| 92 | + [TestCase] |
| 93 | + public void GetPackSummaryIgnoresNonPackFiles() |
| 94 | + { |
| 95 | + string packDir = Path.Combine(this.testDir, "pack"); |
| 96 | + Directory.CreateDirectory(packDir); |
| 97 | + |
| 98 | + this.CreateFileWithSize(Path.Combine(packDir, "prefetch-1000-aabb.pack"), 100); |
| 99 | + this.CreateFileWithSize(Path.Combine(packDir, "prefetch-1000-aabb.idx"), 50); |
| 100 | + this.CreateFileWithSize(Path.Combine(packDir, "multi-pack-index"), 10); |
| 101 | + |
| 102 | + this.cacheVerb.GetPackSummary( |
| 103 | + packDir, |
| 104 | + out int prefetchCount, |
| 105 | + out long prefetchSize, |
| 106 | + out int otherCount, |
| 107 | + out long otherSize, |
| 108 | + out long latestTimestamp); |
| 109 | + |
| 110 | + Assert.AreEqual(1, prefetchCount); |
| 111 | + Assert.AreEqual(100, prefetchSize); |
| 112 | + Assert.AreEqual(0, otherCount); |
| 113 | + Assert.AreEqual(0, otherSize); |
| 114 | + } |
| 115 | + |
| 116 | + [TestCase] |
| 117 | + public void GetPackSummaryHandlesBothGuidAndSHA1HashFormats() |
| 118 | + { |
| 119 | + string packDir = Path.Combine(this.testDir, "pack"); |
| 120 | + Directory.CreateDirectory(packDir); |
| 121 | + |
| 122 | + // GVFS format: 32-char GUID |
| 123 | + this.CreateFileWithSize(Path.Combine(packDir, "prefetch-1000-b8d9efad32194d98894532905daf88ec.pack"), 100); |
| 124 | + // Scalar format: 40-char SHA1 |
| 125 | + this.CreateFileWithSize(Path.Combine(packDir, "prefetch-2000-9babd9b75521f9caf693b485329d3d5669c88564.pack"), 200); |
| 126 | + |
| 127 | + this.cacheVerb.GetPackSummary( |
| 128 | + packDir, |
| 129 | + out int prefetchCount, |
| 130 | + out long prefetchSize, |
| 131 | + out int otherCount, |
| 132 | + out long otherSize, |
| 133 | + out long latestTimestamp); |
| 134 | + |
| 135 | + Assert.AreEqual(2, prefetchCount); |
| 136 | + Assert.AreEqual(300, prefetchSize); |
| 137 | + Assert.AreEqual(2000, latestTimestamp); |
| 138 | + } |
| 139 | + |
| 140 | + [TestCase] |
| 141 | + public void CountLooseObjectsWithNoObjects() |
| 142 | + { |
| 143 | + int count = this.cacheVerb.CountLooseObjects(this.testDir); |
| 144 | + Assert.AreEqual(0, count); |
| 145 | + } |
| 146 | + |
| 147 | + [TestCase] |
| 148 | + public void CountLooseObjectsCountsFilesInHexDirectories() |
| 149 | + { |
| 150 | + Directory.CreateDirectory(Path.Combine(this.testDir, "00")); |
| 151 | + File.WriteAllText(Path.Combine(this.testDir, "00", "abc123"), string.Empty); |
| 152 | + File.WriteAllText(Path.Combine(this.testDir, "00", "def456"), string.Empty); |
| 153 | + |
| 154 | + Directory.CreateDirectory(Path.Combine(this.testDir, "ff")); |
| 155 | + File.WriteAllText(Path.Combine(this.testDir, "ff", "789abc"), string.Empty); |
| 156 | + |
| 157 | + int count = this.cacheVerb.CountLooseObjects(this.testDir); |
| 158 | + Assert.AreEqual(3, count); |
| 159 | + } |
| 160 | + |
| 161 | + [TestCase] |
| 162 | + public void CountLooseObjectsIgnoresNonHexDirectories() |
| 163 | + { |
| 164 | + // "pack" and "info" are valid directories in a git objects dir but not hex dirs |
| 165 | + Directory.CreateDirectory(Path.Combine(this.testDir, "pack")); |
| 166 | + File.WriteAllText(Path.Combine(this.testDir, "pack", "somefile"), string.Empty); |
| 167 | + |
| 168 | + Directory.CreateDirectory(Path.Combine(this.testDir, "info")); |
| 169 | + File.WriteAllText(Path.Combine(this.testDir, "info", "somefile"), string.Empty); |
| 170 | + |
| 171 | + // "ab" is a valid hex dir |
| 172 | + Directory.CreateDirectory(Path.Combine(this.testDir, "ab")); |
| 173 | + File.WriteAllText(Path.Combine(this.testDir, "ab", "object1"), string.Empty); |
| 174 | + |
| 175 | + int count = this.cacheVerb.CountLooseObjects(this.testDir); |
| 176 | + Assert.AreEqual(1, count); |
| 177 | + } |
| 178 | + |
| 179 | + private void CreateFileWithSize(string path, int size) |
| 180 | + { |
| 181 | + byte[] data = new byte[size]; |
| 182 | + File.WriteAllBytes(path, data); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments