forked from Praqma/config-rotator-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-17604] Added test suite for Git
- Loading branch information
1 parent
3612e72
commit 7ed417b
Showing
6 changed files
with
231 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/test/java/net/praqma/jenkins/configrotator/GitRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package net.praqma.jenkins.configrotator; | ||
|
||
import hudson.FilePath; | ||
import org.apache.commons.io.FileUtils; | ||
import org.eclipse.jgit.api.Git; | ||
import org.eclipse.jgit.api.errors.GitAPIException; | ||
import org.eclipse.jgit.api.errors.NoFilepatternException; | ||
import org.eclipse.jgit.lib.Repository; | ||
import org.eclipse.jgit.revwalk.RevCommit; | ||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder; | ||
import org.junit.rules.TestRule; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* @author cwolfgang | ||
*/ | ||
public class GitRule implements TestRule { | ||
|
||
private static Logger logger = Logger.getLogger( GitRule.class.getName() ); | ||
|
||
private File gitPath; | ||
private Repository gitRepo; | ||
private Git git; | ||
|
||
public void initialize( File gitPath ) { | ||
this.gitPath = gitPath; | ||
|
||
FileRepositoryBuilder builder = new FileRepositoryBuilder(); | ||
try { | ||
gitRepo = builder.setGitDir( new File( gitPath, ".git" ) ).build(); | ||
gitRepo.create(); | ||
} catch( IOException e ) { | ||
e.printStackTrace(); | ||
} | ||
git = new Git( gitRepo ); | ||
} | ||
|
||
public RevCommit createCommit( String filename, String content ) throws IOException, GitAPIException { | ||
|
||
File file = new File( gitPath, filename ); | ||
boolean create = !file.exists(); | ||
FileUtils.write( file, content, false ); | ||
|
||
git.add().addFilepattern( filename ).call(); | ||
return git.commit().setMessage( ( create ? "Creating " : "Updating" ) + " " + filename ).setAll( true ).call(); | ||
} | ||
|
||
public String getRepo() { | ||
return gitRepo.getDirectory().getAbsolutePath(); | ||
} | ||
|
||
|
||
@Override | ||
public Statement apply( final Statement base, Description description ) { | ||
return new Statement() { | ||
|
||
@Override | ||
public void evaluate() throws Throwable { | ||
base.evaluate(); | ||
} | ||
}; | ||
} | ||
|
||
protected void listPath( File path ) { | ||
logger.info( "Listing " + path + "(" + path.exists() + ")" ); | ||
for( String f : path.list() ) { | ||
logger.info( " * " + f ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters