001package org.apache.maven.scm.provider.git; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023import java.io.FileWriter; 024import java.io.IOException; 025 026import org.junit.Assert; 027 028import org.codehaus.plexus.PlexusTestCase; 029import org.codehaus.plexus.util.FileUtils; 030import org.codehaus.plexus.util.cli.CommandLineException; 031 032/** 033 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> 034 * 035 */ 036public final class GitScmTestUtils 037{ 038 private GitScmTestUtils() 039 { 040 } 041 042 public static void initRepo( File repository, File workingDirectory, File assertionDirectory ) 043 throws IOException 044 { 045 initRepo( "src/test/repository/", repository, workingDirectory ); 046 047 FileUtils.deleteDirectory( assertionDirectory ); 048 049 Assert.assertTrue( assertionDirectory.mkdirs() ); 050 } 051 052 public static void initRepo( String source, File repository, File workingDirectory ) 053 throws IOException 054 { 055 // Copy the repository to target 056 File src = PlexusTestCase.getTestFile( source ); 057 058 FileUtils.deleteDirectory( repository ); 059 060 Assert.assertTrue( repository.mkdirs() ); 061 062 FileUtils.copyDirectoryStructure( src, repository ); 063 064 File dotGitDirectory = new File( src, "dotgit" ); 065 066 if ( dotGitDirectory.exists() ) 067 { 068 FileUtils.copyDirectoryStructure( dotGitDirectory, new File( repository, ".git" ) ); 069 } 070 071 FileUtils.deleteDirectory( workingDirectory ); 072 073 Assert.assertTrue( workingDirectory.mkdirs() ); 074 } 075 076 public static String getScmUrl( File repositoryRootFile, String provider ) 077 throws CommandLineException 078 { 079 return "scm:" + provider + ":" + repositoryRootFile.toPath().toAbsolutePath().toUri().toASCIIString(); 080 } 081 082 083 public static void deleteAllDirectories( File startDirectory, String pattern ) 084 throws IOException 085 { 086 if ( startDirectory.isDirectory() ) 087 { 088 File[] childs = startDirectory.listFiles(); 089 for ( int i = 0; i < childs.length; i++ ) 090 { 091 File child = childs[i]; 092 if ( child.isDirectory() ) 093 { 094 if ( child.getName().equals( pattern ) ) 095 { 096 FileUtils.deleteDirectory( child ); 097 } 098 else 099 { 100 deleteAllDirectories( child, pattern ); 101 } 102 } 103 } 104 } 105 } 106 107 public static void setDefaultUser( File repositoryRootFile ) 108 { 109 File gitConfigFile = new File( new File( repositoryRootFile, ".git" ), "config" ); 110 111 FileWriter fw = null; 112 try 113 { 114 fw = new FileWriter( gitConfigFile , true ); 115 fw.append( "[user]\n" ); 116 fw.append( "\tname = John Doe\n" ); 117 fw.append( "\temail = john.doe@nowhere.com\n" ); 118 fw.flush(); 119 fw.close(); 120 } 121 catch ( IOException e ) 122 { 123 System.err.println( "cannot setup a default user for tests purpose inside " + gitConfigFile ); 124 e.printStackTrace(); 125 } 126 finally 127 { 128 if ( fw != null ) 129 { 130 try 131 { 132 fw.close(); 133 } 134 catch ( IOException ignore ) 135 { 136 // ignored 137 } 138 } 139 } 140 } 141}