You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AttributeFileTests.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 2020 Thomas Wolf <thomas.wolf@paranor.ch> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.attributes;
  11. import static org.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertTrue;
  14. import static org.junit.Assert.fail;
  15. import java.io.BufferedInputStream;
  16. import java.io.File;
  17. import java.io.InputStream;
  18. import java.nio.charset.StandardCharsets;
  19. import java.nio.file.Files;
  20. import java.util.Arrays;
  21. import org.eclipse.jgit.api.Git;
  22. import org.eclipse.jgit.api.ResetCommand.ResetType;
  23. import org.eclipse.jgit.dircache.DirCache;
  24. import org.eclipse.jgit.dircache.DirCacheEntry;
  25. import org.eclipse.jgit.junit.RepositoryTestCase;
  26. import org.eclipse.jgit.lib.ConfigConstants;
  27. import org.eclipse.jgit.lib.Constants;
  28. import org.eclipse.jgit.storage.file.FileBasedConfig;
  29. import org.eclipse.jgit.util.IO;
  30. import org.eclipse.jgit.util.RawParseUtils;
  31. import org.junit.Test;
  32. /**
  33. * End-to-end tests for some attribute combinations. Writes files, commit them,
  34. * examines the index, deletes the files, performs a hard reset and checks file
  35. * contents again.
  36. */
  37. public class AttributeFileTests extends RepositoryTestCase {
  38. @Test
  39. public void testTextAutoCoreEolCoreAutoCrLfInput() throws Exception {
  40. FileBasedConfig cfg = db.getConfig();
  41. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  42. ConfigConstants.CONFIG_KEY_AUTOCRLF, false);
  43. cfg.save();
  44. final String content = "Line1\nLine2\n";
  45. try (Git git = Git.wrap(db)) {
  46. writeTrashFile(".gitattributes", "* text=auto");
  47. File dummy = writeTrashFile("dummy.txt", content);
  48. git.add().addFilepattern(".").call();
  49. git.commit().setMessage("Commit with LF").call();
  50. assertEquals("Unexpected index state",
  51. "[.gitattributes, mode:100644, content:* text=auto]"
  52. + "[dummy.txt, mode:100644, content:" + content
  53. + ']',
  54. indexState(CONTENT));
  55. assertTrue("Should be able to delete " + dummy, dummy.delete());
  56. cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  57. ConfigConstants.CONFIG_KEY_EOL, "crlf");
  58. cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  59. ConfigConstants.CONFIG_KEY_AUTOCRLF, "input");
  60. cfg.save();
  61. git.reset().setMode(ResetType.HARD).call();
  62. assertTrue("File " + dummy + "should exist", dummy.isFile());
  63. String textFile = RawParseUtils.decode(IO.readFully(dummy, 512));
  64. assertEquals("Unexpected text content", content, textFile);
  65. }
  66. }
  67. @Test
  68. public void testTextAutoEolLf() throws Exception {
  69. writeTrashFile(".gitattributes", "* text=auto eol=lf");
  70. performTest("Test\r\nFile", "Test\nFile", "Test\nFile");
  71. }
  72. @Test
  73. public void testTextAutoEolCrLf() throws Exception {
  74. writeTrashFile(".gitattributes", "* text=auto eol=crlf");
  75. performTest("Test\r\nFile", "Test\nFile", "Test\r\nFile");
  76. }
  77. private void performTest(String initial, String index, String finalText)
  78. throws Exception {
  79. File dummy = writeTrashFile("dummy.foo", initial);
  80. byte[] data = readTestResource("add.png");
  81. assertTrue("Expected some binary data", data.length > 100);
  82. File binary = writeTrashFile("add.png", "");
  83. Files.write(binary.toPath(), data);
  84. try (Git git = Git.wrap(db)) {
  85. git.add().addFilepattern(".").call();
  86. git.commit().setMessage("test commit").call();
  87. // binary should be unchanged, dummy should match "index"
  88. verifyIndexContent("dummy.foo",
  89. index.getBytes(StandardCharsets.UTF_8));
  90. verifyIndexContent("add.png", data);
  91. assertTrue("Should be able to delete " + dummy, dummy.delete());
  92. assertTrue("Should be able to delete " + binary, binary.delete());
  93. git.reset().setMode(ResetType.HARD).call();
  94. assertTrue("File " + dummy + " should exist", dummy.isFile());
  95. assertTrue("File " + binary + " should exist", binary.isFile());
  96. // binary should be unchanged, dummy should match "finalText"
  97. String textFile = RawParseUtils.decode(IO.readFully(dummy, 512));
  98. assertEquals("Unexpected text content", finalText, textFile);
  99. byte[] binaryFile = IO.readFully(binary, 512);
  100. assertArrayEquals("Unexpected binary content", data, binaryFile);
  101. }
  102. }
  103. private byte[] readTestResource(String name) throws Exception {
  104. try (InputStream in = new BufferedInputStream(
  105. getClass().getResourceAsStream(name))) {
  106. byte[] data = new byte[512];
  107. int read = in.read(data);
  108. if (read == data.length) {
  109. return data;
  110. }
  111. return Arrays.copyOf(data, read);
  112. }
  113. }
  114. private void verifyIndexContent(String path, byte[] expectedContent)
  115. throws Exception {
  116. DirCache dc = db.readDirCache();
  117. for (int i = 0; i < dc.getEntryCount(); ++i) {
  118. DirCacheEntry entry = dc.getEntry(i);
  119. if (path.equals(entry.getPathString())) {
  120. byte[] data = db.open(entry.getObjectId(), Constants.OBJ_BLOB)
  121. .getCachedBytes();
  122. assertArrayEquals("Unexpected index content for " + path,
  123. expectedContent, data);
  124. return;
  125. }
  126. }
  127. fail("Path not found in index: " + path);
  128. }
  129. }