@ -71,6 +71,23 @@ import org.junit.Assume;
import org.junit.Test ;
import org.junit.Test ;
public class CheckoutTest extends CLIRepositoryTestCase {
public class CheckoutTest extends CLIRepositoryTestCase {
/ * *
* Executes specified git command ( with arguments ) , captures exception and
* returns its message as an array of lines . Throws an AssertionError if no
* exception is thrown .
*
* @param command
* a valid git command line , e . g . "git branch -h"
* @return message contained within the exception
* /
private String [ ] executeExpectingException ( String command ) {
try {
execute ( command ) ;
throw new AssertionError ( "Expected Die" ) ;
} catch ( Exception e ) {
return e . getMessage ( ) . split ( System . lineSeparator ( ) ) ;
}
}
@Test
@Test
public void testCheckoutSelf ( ) throws Exception {
public void testCheckoutSelf ( ) throws Exception {
@ -107,7 +124,7 @@ public class CheckoutTest extends CLIRepositoryTestCase {
public void testCheckoutNonExistingBranch ( ) throws Exception {
public void testCheckoutNonExistingBranch ( ) throws Exception {
assertStringArrayEquals (
assertStringArrayEquals (
"error: pathspec 'side' did not match any file(s) known to git." ,
"error: pathspec 'side' did not match any file(s) known to git." ,
execute ( "git checkout side" ) ) ;
executeExpectingException ( "git checkout side" ) ) ;
}
}
@Test
@Test
@ -131,7 +148,7 @@ public class CheckoutTest extends CLIRepositoryTestCase {
public void testCheckoutUnresolvedHead ( ) throws Exception {
public void testCheckoutUnresolvedHead ( ) throws Exception {
assertStringArrayEquals (
assertStringArrayEquals (
"error: pathspec 'HEAD' did not match any file(s) known to git." ,
"error: pathspec 'HEAD' did not match any file(s) known to git." ,
execute ( "git checkout HEAD" ) ) ;
executeExpectingException ( "git checkout HEAD" ) ) ;
}
}
@Test
@Test
@ -159,7 +176,8 @@ public class CheckoutTest extends CLIRepositoryTestCase {
writeTrashFile ( "a" , "New Hello world a" ) ;
writeTrashFile ( "a" , "New Hello world a" ) ;
git . add ( ) . addFilepattern ( "." ) . call ( ) ;
git . add ( ) . addFilepattern ( "." ) . call ( ) ;
String [ ] execute = execute ( "git checkout branch_1" ) ;
String [ ] execute = executeExpectingException (
"git checkout branch_1" ) ;
assertEquals (
assertEquals (
"error: Your local changes to the following files would be overwritten by checkout:" ,
"error: Your local changes to the following files would be overwritten by checkout:" ,
execute [ 0 ] ) ;
execute [ 0 ] ) ;