File: DiffFormatterTest.cs

package info (click to toggle)
monodevelop 3.0.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 153,256 kB
  • sloc: cs: 1,020,242; xml: 751,053; makefile: 9,596; sh: 1,529; objc: 302; sql: 129; ansic: 96
file content (283 lines) | stat: -rwxr-xr-x 11,096 bytes parent folder | download | duplicates (2)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
This code is derived from jgit (http://eclipse.org/jgit).
Copyright owners are documented in jgit's IP log.

This program and the accompanying materials are made available
under the terms of the Eclipse Distribution License v1.0 which
accompanies this distribution, is reproduced below, and is
available at http://www.eclipse.org/org/documents/edl-v10.php

All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:

- Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the following
  disclaimer in the documentation and/or other materials provided
  with the distribution.

- Neither the name of the Eclipse Foundation, Inc. nor the
  names of its contributors may be used to endorse or promote
  products derived from this software without specific prior
  written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using NGit;
using NGit.Api;
using NGit.Diff;
using NGit.Dircache;
using NGit.Junit;
using NGit.Patch;
using NGit.Treewalk;
using NGit.Treewalk.Filter;
using NGit.Util;
using NGit.Util.IO;
using Sharpen;
using NGit.Storage.File;

namespace NGit.Diff
{
	[NUnit.Framework.TestFixture]
	public class DiffFormatterTest : RepositoryTestCase
	{
		private static readonly string DIFF = "diff --git ";

		private static readonly string REGULAR_FILE = "100644";

		private static readonly string GITLINK = "160000";

		private static readonly string PATH_A = "src/a";

		private static readonly string PATH_B = "src/b";

		private DiffFormatter df;

		private TestRepository testDb;

		/// <exception cref="System.Exception"></exception>
		[NUnit.Framework.SetUp]
		public override void SetUp()
		{
			base.SetUp();
			testDb = new TestRepository<FileRepository>(db);
			df = new DiffFormatter(DisabledOutputStream.INSTANCE);
			df.SetRepository(db);
			df.SetAbbreviationLength(8);
		}

		/// <exception cref="System.Exception"></exception>
		[NUnit.Framework.TearDown]
		public override void TearDown()
		{
			if (df != null)
			{
				df.Release();
			}
			base.TearDown();
		}

		/// <exception cref="System.Exception"></exception>
		[NUnit.Framework.Test]
		public virtual void TestCreateFileHeader_Add()
		{
			ObjectId adId = Blob("a\nd\n");
			DiffEntry ent = DiffEntry.Add("FOO", adId);
			FileHeader fh = df.ToFileHeader(ent);
			string diffHeader = "diff --git a/FOO b/FOO\n" + "new file mode " + REGULAR_FILE 
				+ "\n" + "index " + ObjectId.ZeroId.Abbreviate(8).Name + ".." + adId.Abbreviate(
				8).Name + "\n" + "--- /dev/null\n" + "+++ b/FOO\n";
			//
			//
			//
			NUnit.Framework.Assert.AreEqual(diffHeader, RawParseUtils.Decode(fh.GetBuffer()));
			NUnit.Framework.Assert.AreEqual(0, fh.GetStartOffset());
			NUnit.Framework.Assert.AreEqual(fh.GetBuffer().Length, fh.GetEndOffset());
			NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, fh.GetPatchType());
			NUnit.Framework.Assert.AreEqual(1, fh.GetHunks().Count);
			HunkHeader hh = fh.GetHunks()[0];
			NUnit.Framework.Assert.AreEqual(1, hh.ToEditList().Count);
			EditList el = hh.ToEditList();
			NUnit.Framework.Assert.AreEqual(1, el.Count);
			Edit e = el[0];
			NUnit.Framework.Assert.AreEqual(0, e.GetBeginA());
			NUnit.Framework.Assert.AreEqual(0, e.GetEndA());
			NUnit.Framework.Assert.AreEqual(0, e.GetBeginB());
			NUnit.Framework.Assert.AreEqual(2, e.GetEndB());
			NUnit.Framework.Assert.AreEqual(Edit.Type.INSERT, e.GetType());
		}

		/// <exception cref="System.Exception"></exception>
		[NUnit.Framework.Test]
		public virtual void TestCreateFileHeader_Delete()
		{
			ObjectId adId = Blob("a\nd\n");
			DiffEntry ent = DiffEntry.Delete("FOO", adId);
			FileHeader fh = df.ToFileHeader(ent);
			string diffHeader = "diff --git a/FOO b/FOO\n" + "deleted file mode " + REGULAR_FILE
				 + "\n" + "index " + adId.Abbreviate(8).Name + ".." + ObjectId.ZeroId.Abbreviate
				(8).Name + "\n" + "--- a/FOO\n" + "+++ /dev/null\n";
			//
			//
			//
			NUnit.Framework.Assert.AreEqual(diffHeader, RawParseUtils.Decode(fh.GetBuffer()));
			NUnit.Framework.Assert.AreEqual(0, fh.GetStartOffset());
			NUnit.Framework.Assert.AreEqual(fh.GetBuffer().Length, fh.GetEndOffset());
			NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, fh.GetPatchType());
			NUnit.Framework.Assert.AreEqual(1, fh.GetHunks().Count);
			HunkHeader hh = fh.GetHunks()[0];
			NUnit.Framework.Assert.AreEqual(1, hh.ToEditList().Count);
			EditList el = hh.ToEditList();
			NUnit.Framework.Assert.AreEqual(1, el.Count);
			Edit e = el[0];
			NUnit.Framework.Assert.AreEqual(0, e.GetBeginA());
			NUnit.Framework.Assert.AreEqual(2, e.GetEndA());
			NUnit.Framework.Assert.AreEqual(0, e.GetBeginB());
			NUnit.Framework.Assert.AreEqual(0, e.GetEndB());
			NUnit.Framework.Assert.AreEqual(Edit.Type.DELETE, e.GetType());
		}

		/// <exception cref="System.Exception"></exception>
		[NUnit.Framework.Test]
		public virtual void TestCreateFileHeader_Modify()
		{
			ObjectId adId = Blob("a\nd\n");
			ObjectId abcdId = Blob("a\nb\nc\nd\n");
			string diffHeader = MakeDiffHeader(PATH_A, PATH_A, adId, abcdId);
			DiffEntry ad = DiffEntry.Delete(PATH_A, adId);
			DiffEntry abcd = DiffEntry.Add(PATH_A, abcdId);
			DiffEntry mod = DiffEntry.Pair(DiffEntry.ChangeType.MODIFY, ad, abcd, 0);
			FileHeader fh = df.ToFileHeader(mod);
			NUnit.Framework.Assert.AreEqual(diffHeader, RawParseUtils.Decode(fh.GetBuffer()));
			NUnit.Framework.Assert.AreEqual(0, fh.GetStartOffset());
			NUnit.Framework.Assert.AreEqual(fh.GetBuffer().Length, fh.GetEndOffset());
			NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, fh.GetPatchType());
			NUnit.Framework.Assert.AreEqual(1, fh.GetHunks().Count);
			HunkHeader hh = fh.GetHunks()[0];
			NUnit.Framework.Assert.AreEqual(1, hh.ToEditList().Count);
			EditList el = hh.ToEditList();
			NUnit.Framework.Assert.AreEqual(1, el.Count);
			Edit e = el[0];
			NUnit.Framework.Assert.AreEqual(1, e.GetBeginA());
			NUnit.Framework.Assert.AreEqual(1, e.GetEndA());
			NUnit.Framework.Assert.AreEqual(1, e.GetBeginB());
			NUnit.Framework.Assert.AreEqual(3, e.GetEndB());
			NUnit.Framework.Assert.AreEqual(Edit.Type.INSERT, e.GetType());
		}

		/// <exception cref="System.Exception"></exception>
		[NUnit.Framework.Test]
		public virtual void TestCreateFileHeader_Binary()
		{
			ObjectId adId = Blob("a\nd\n");
			ObjectId binId = Blob("a\nb\nc\n\x0\x0\x0\x0d\n");
			string diffHeader = MakeDiffHeader(PATH_A, PATH_B, adId, binId) + "Binary files differ\n";
			DiffEntry ad = DiffEntry.Delete(PATH_A, adId);
			DiffEntry abcd = DiffEntry.Add(PATH_B, binId);
			DiffEntry mod = DiffEntry.Pair(DiffEntry.ChangeType.MODIFY, ad, abcd, 0);
			FileHeader fh = df.ToFileHeader(mod);
			NUnit.Framework.Assert.AreEqual(diffHeader, RawParseUtils.Decode(fh.GetBuffer()));
			NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.BINARY, fh.GetPatchType());
			NUnit.Framework.Assert.AreEqual(1, fh.GetHunks().Count);
			HunkHeader hh = fh.GetHunks()[0];
			NUnit.Framework.Assert.AreEqual(0, hh.ToEditList().Count);
		}

		/// <exception cref="System.Exception"></exception>
		[NUnit.Framework.Test]
		public virtual void TestCreateFileHeader_GitLink()
		{
			ObjectId aId = Blob("a\n");
			ObjectId bId = Blob("b\n");
			string diffHeader = MakeDiffHeaderModeChange(PATH_A, PATH_A, aId, bId, GITLINK, REGULAR_FILE
				) + "-Subproject commit " + aId.Name + "\n";
			DiffEntry ad = DiffEntry.Delete(PATH_A, aId);
			ad.oldMode = FileMode.GITLINK;
			DiffEntry abcd = DiffEntry.Add(PATH_A, bId);
			DiffEntry mod = DiffEntry.Pair(DiffEntry.ChangeType.MODIFY, ad, abcd, 0);
			FileHeader fh = df.ToFileHeader(mod);
			NUnit.Framework.Assert.AreEqual(diffHeader, RawParseUtils.Decode(fh.GetBuffer()));
			NUnit.Framework.Assert.AreEqual(1, fh.GetHunks().Count);
			HunkHeader hh = fh.GetHunks()[0];
			NUnit.Framework.Assert.AreEqual(0, hh.ToEditList().Count);
		}

		/// <exception cref="System.Exception"></exception>
		[NUnit.Framework.Test]
		public virtual void TestDiff()
		{
			Write(new FilePath(db.Directory.GetParent(), "test.txt"), "test");
			FilePath folder = new FilePath(db.Directory.GetParent(), "folder");
			folder.Mkdir();
			Write(new FilePath(folder, "folder.txt"), "folder");
			Git git = new Git(db);
			git.Add().AddFilepattern(".").Call();
			git.Commit().SetMessage("Initial commit").Call();
			Write(new FilePath(folder, "folder.txt"), "folder change");
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			DiffFormatter df = new DiffFormatter(new SafeBufferedOutputStream(os));
			df.SetRepository(db);
			df.SetPathFilter(PathFilter.Create("folder"));
			DirCacheIterator oldTree = new DirCacheIterator(db.ReadDirCache());
			FileTreeIterator newTree = new FileTreeIterator(db);
			df.Format(oldTree, newTree);
			df.Flush();
			string actual = System.Text.Encoding.UTF8.GetString (os.ToByteArray ());
			string expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" + "index 0119635..95c4c65 100644\n"
				 + "--- a/folder/folder.txt\n" + "+++ b/folder/folder.txt\n" + "@@ -1 +1 @@\n" +
				 "-folder\n" + "\\ No newline at end of file\n" + "+folder change\n" + "\\ No newline at end of file\n";
			NUnit.Framework.Assert.AreEqual(expected.ToString(), actual);
		}

		private string MakeDiffHeader(string pathA, string pathB, ObjectId aId, ObjectId 
			bId)
		{
			string a = aId.Abbreviate(8).Name;
			string b = bId.Abbreviate(8).Name;
			return DIFF + "a/" + pathA + " " + "b/" + pathB + "\n" + "index " + a + ".." + b 
				+ " " + REGULAR_FILE + "\n" + "--- a/" + pathA + "\n" + "+++ b/" + pathB + "\n";
		}

		//
		//
		//
		private string MakeDiffHeaderModeChange(string pathA, string pathB, ObjectId aId, 
			ObjectId bId, string modeA, string modeB)
		{
			string a = aId.Abbreviate(8).Name;
			string b = bId.Abbreviate(8).Name;
			return DIFF + "a/" + pathA + " " + "b/" + pathB + "\n" + "old mode " + modeA + "\n"
				 + "new mode " + modeB + "\n" + "index " + a + ".." + b + "\n" + "--- a/" + pathA
				 + "\n" + "+++ b/" + pathB + "\n";
		}

		//
		//
		//
		//
		//
		/// <exception cref="System.Exception"></exception>
		private ObjectId Blob(string content)
		{
			return testDb.Blob(content).Copy();
		}
	}
}