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.

DescribeTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2013, Matthias Sohn <matthias.sohn@sap.com> 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.pgm;
  11. import static org.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.util.Arrays;
  17. import org.eclipse.jgit.api.Git;
  18. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  19. import org.eclipse.jgit.pgm.internal.CLIText;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. public class DescribeTest extends CLIRepositoryTestCase {
  23. private Git git;
  24. @Override
  25. @Before
  26. public void setUp() throws Exception {
  27. super.setUp();
  28. git = new Git(db);
  29. }
  30. private void initialCommitAndTag() throws Exception {
  31. git.commit().setMessage("initial commit").call();
  32. git.tag().setName("v1.0").call();
  33. }
  34. private void secondCommit() throws Exception {
  35. writeTrashFile("greeting", "Hello, world!");
  36. git.add().addFilepattern("greeting").call();
  37. git.commit().setMessage("2nd commit").call();
  38. }
  39. @Test
  40. public void testNoHead() throws Exception {
  41. assertEquals(CLIText.fatalError(CLIText.get().noNamesFound),
  42. toString(executeUnchecked("git describe")));
  43. }
  44. @Test
  45. public void testHeadNoTag() throws Exception {
  46. git.commit().setMessage("initial commit").call();
  47. assertEquals(CLIText.fatalError(CLIText.get().noNamesFound),
  48. toString(executeUnchecked("git describe")));
  49. }
  50. @Test
  51. public void testDescribeTag() throws Exception {
  52. initialCommitAndTag();
  53. assertArrayEquals(new String[] { "v1.0", "" },
  54. execute("git describe HEAD"));
  55. }
  56. @Test
  57. public void testDescribeCommit() throws Exception {
  58. initialCommitAndTag();
  59. secondCommit();
  60. assertArrayEquals(new String[] { "v1.0-1-g56f6ceb", "" },
  61. execute("git describe"));
  62. }
  63. @Test
  64. public void testDescribeTagLong() throws Exception {
  65. initialCommitAndTag();
  66. assertArrayEquals(new String[] { "v1.0-0-g6fd41be", "" },
  67. execute("git describe --long HEAD"));
  68. }
  69. @Test
  70. public void testDescribeCommitMatch() throws Exception {
  71. initialCommitAndTag();
  72. secondCommit();
  73. assertArrayEquals(new String[] { "v1.0-1-g56f6ceb", "" },
  74. execute("git describe --match v1.*"));
  75. }
  76. @Test
  77. public void testDescribeCommitMatch2() throws Exception {
  78. initialCommitAndTag();
  79. secondCommit();
  80. git.tag().setName("v2.0").call();
  81. assertArrayEquals(new String[] { "v1.0-1-g56f6ceb", "" },
  82. execute("git describe --match v1.*"));
  83. }
  84. @Test
  85. public void testDescribeCommitMultiMatch() throws Exception {
  86. initialCommitAndTag();
  87. secondCommit();
  88. git.tag().setName("v2.0.0").call();
  89. git.tag().setName("v2.1.1").call();
  90. assertArrayEquals("git yields v2.0.0", new String[] { "v2.0.0", "" },
  91. execute("git describe --match v2.0* --match v2.1.*"));
  92. }
  93. @Test
  94. public void testDescribeCommitNoMatch() throws Exception {
  95. initialCommitAndTag();
  96. writeTrashFile("greeting", "Hello, world!");
  97. secondCommit();
  98. try {
  99. execute("git describe --match 1.*");
  100. fail("git describe should not find any tag matching 1.*");
  101. } catch (Die e) {
  102. assertEquals("No names found, cannot describe anything.",
  103. e.getMessage());
  104. }
  105. }
  106. @Test
  107. public void testHelpArgumentBeforeUnknown() throws Exception {
  108. String[] output = execute("git describe -h -XYZ");
  109. String all = Arrays.toString(output);
  110. assertTrue("Unexpected help output: " + all,
  111. all.contains("jgit describe"));
  112. assertFalse("Unexpected help output: " + all, all.contains("fatal"));
  113. }
  114. @Test
  115. public void testHelpArgumentAfterUnknown() throws Exception {
  116. String[] output = executeUnchecked("git describe -XYZ -h");
  117. String all = Arrays.toString(output);
  118. assertTrue("Unexpected help output: " + all,
  119. all.contains("jgit describe"));
  120. assertTrue("Unexpected help output: " + all, all.contains("fatal"));
  121. }
  122. }