@@ -5,7 +5,7 @@ var fse = promisify(require("fs-extra"));
55
66var exec = require ( "../../utils/execPromise" ) ;
77
8- describe ( "Stage" , function ( ) {
8+ describe . only ( "Stage" , function ( ) {
99 var RepoUtils = require ( "../utils/repository_setup" ) ;
1010 var NodeGit = require ( "../../" ) ;
1111 var test ;
@@ -25,7 +25,7 @@ describe("Stage", function() {
2525 return fse . remove ( test . repository . workdir ( ) ) ;
2626 } ) ;
2727
28- function stagingTest ( staging , newFileContent ) {
28+ function stagingTest ( isUnstaged , newFileContent , discarding ) {
2929 var fileContent = newFileContent ||
3030 "One line of text\n" +
3131 "Two lines of text\n" +
@@ -48,13 +48,22 @@ describe("Stage", function() {
4848 "Nineteen lines of text\n" +
4949 "Twenty lines of text\n" ;
5050 var fileName = "stagedLinesTest.txt" ;
51- var stagedFile ;
51+ var expectedContent ;
5252 var workingDirFile ;
5353 var getDiffFunction ;
54- if ( staging ) {
55- stagedFile = fileContent . replace ( "Fifteen" , "Changed fifteen" ) ;
56- workingDirFile = stagedFile . replace ( "Three" , "Changed three" )
54+
55+ if ( ! isUnstaged || discarding ) {
56+ expectedContent = fileContent . replace ( "Three" , "Changed three" )
5757 . replace ( "Seventeen" , "Changed seventeen" ) ;
58+ workingDirFile = expectedContent . replace ( "Fifteen" , "Changed fifteen" ) ;
59+ }
60+ else {
61+ expectedContent = fileContent . replace ( "Fifteen" , "Changed fifteen" ) ;
62+ workingDirFile = expectedContent . replace ( "Three" , "Changed three" )
63+ . replace ( "Seventeen" , "Changed seventeen" ) ;
64+ }
65+
66+ if ( isUnstaged ) {
5867 getDiffFunction = function ( ) {
5968 return test . repository . refreshIndex ( )
6069 . then ( function ( index ) {
@@ -71,10 +80,6 @@ describe("Stage", function() {
7180 } ;
7281 }
7382 else {
74- stagedFile = fileContent . replace ( "Three" , "Changed three" )
75- . replace ( "Seventeen" , "Changed seventeen" ) ;
76- workingDirFile = stagedFile . replace ( "Fifteen" , "Changed fifteen" ) ;
77-
7883 getDiffFunction = function ( ) {
7984 return RepoUtils . addFileToIndex ( test . repository , fileName )
8085 . then ( function ( ) {
@@ -138,17 +143,28 @@ describe("Stage", function() {
138143 }
139144 } ) ;
140145 } ) ;
141- return test . repository . stageLines ( fileName , linesToStage , ! staging ) ;
146+
147+ if ( discarding ) {
148+ return test . repository . discardLines ( fileName , linesToStage ) ;
149+ }
150+
151+ return test . repository . stageLines ( fileName , linesToStage , ! isUnstaged ) ;
142152 } )
143153 . then ( function ( ) {
144- return test . repository . refreshIndex ( ) ;
145- } )
146- . then ( function ( reloadedIndex ) {
147- var pathOid = reloadedIndex . getByPath ( fileName ) . id ;
148- return test . repository . getBlob ( pathOid ) ;
154+ if ( discarding ) {
155+ return fse . readFile (
156+ path . join ( test . repository . workdir ( ) , fileName ) , "utf8"
157+ ) ;
158+ }
159+
160+ return test . repository . refreshIndex ( )
161+ . then ( function ( reloadedIndex ) {
162+ var pathOid = reloadedIndex . getByPath ( fileName ) . id ;
163+ return test . repository . getBlob ( pathOid ) ;
164+ } ) ;
149165 } )
150166 . then ( function ( resultFileContents ) {
151- assert . equal ( resultFileContents . toString ( ) , stagedFile ) ;
167+ assert . equal ( resultFileContents . toString ( ) , expectedContent ) ;
152168 } ) ;
153169 }
154170
@@ -501,4 +517,8 @@ describe("Stage", function() {
501517 return compareFilemodes ( false , freshIndex , 0 /* expect nochange */ ) ;
502518 } ) ;
503519 } ) ;
520+
521+ it ( "can discard selected lines" , function ( ) {
522+ return stagingTest ( true , null , true ) ;
523+ } ) ;
504524} ) ;
0 commit comments