Skip to content

Commit

Permalink
Merged in feature/RET-1756-create-migrate-command (pull request #121)…
Browse files Browse the repository at this point in the history
… by rebazer

Feature/ret 1756 create migrate command
  • Loading branch information
rebazer authored Aug 29, 2019
2 parents 6b1cddd + c06568f commit 9585190
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/de/retest/recheck/cli/RecheckCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import de.retest.recheck.cli.subcommands.Completion;
import de.retest.recheck.cli.subcommands.Diff;
import de.retest.recheck.cli.subcommands.Ignore;
import de.retest.recheck.cli.subcommands.Migrate;
import de.retest.recheck.cli.subcommands.Version;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command( name = "recheck", description = "Command-line interface for recheck.",
versionProvider = VersionProvider.class, subcommands = { Version.class, Diff.class, Commit.class, Ignore.class,
Completion.class, CommandLine.HelpCommand.class } )
Completion.class, CommandLine.HelpCommand.class, Migrate.class } )
public class RecheckCli implements Runnable {

@Option( names = "--help", usageHelp = true, description = "Display this help message." )
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/de/retest/recheck/cli/subcommands/Migrate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package de.retest.recheck.cli.subcommands;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.retest.recheck.Properties;
import de.retest.recheck.configuration.ProjectRootFinderUtil;
import de.retest.recheck.persistence.GoldenMasterProviderImpl;
import de.retest.recheck.persistence.NoGoldenMasterFoundException;
import de.retest.recheck.persistence.Persistence;
import de.retest.recheck.persistence.PersistenceFactory;
import de.retest.recheck.persistence.xml.util.StdXmlClassesProvider;
import de.retest.recheck.ui.descriptors.SutState;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

@Command( name = "migrate", description = "Migrate Golden Master(s)." )
public class Migrate implements Runnable {

private static final String RECHECK_FOLDER = "/src/test/resources/retest/recheck";
private static final Logger logger = LoggerFactory.getLogger( Migrate.class );

@Option( names = "--help", usageHelp = true, hidden = true )
private boolean displayHelp;

@Parameters( arity = "1", defaultValue = "", description = "The path of the Golden Master(s)." )
private String goldenMasterPath;

private final GoldenMasterProviderImpl goldenMasterProvider =
new GoldenMasterProviderImpl( createSutStatePersistence() );

@Override
public void run() {
try {
migrateAllGoldenMasters( getAllGoldenMasters() );
} catch ( final IOException e ) {
logger.error( "The directory '{}' does not exist.", e.getMessage() );
} catch ( final NoGoldenMasterFoundException e ) {
logger.error( "There is no Golden Master(s) in the directory '{}'.", e.getFilename() );
}
}

private void migrateAllGoldenMasters( final List<String> goldenMastersPaths ) throws NoGoldenMasterFoundException {
if ( goldenMastersPaths.isEmpty() ) {
throw new NoGoldenMasterFoundException( goldenMasterPath );
}
goldenMastersPaths.stream().forEach( path -> {
final File goldenMaster = new File( path );
final SutState sutState = goldenMasterProvider.loadGoldenMaster( goldenMaster );
goldenMasterProvider.saveGoldenMaster( goldenMaster, sutState );
} );
}

private List<String> getAllGoldenMasters() throws IOException {
final String path = !goldenMasterPath.equals( "" ) ? goldenMasterPath : RECHECK_FOLDER;
final File goldenMaster = ProjectRootFinderUtil.getProjectRoot()
.map( gmPath -> Paths.get( gmPath.toAbsolutePath().toString(), path ) ).map( Path::toFile )
.orElseThrow( () -> new IOException() );
try ( final Stream<Path> goldenMasterPaths = Files.walk( goldenMaster.toPath() ) ) {
return goldenMasterPaths.filter( gmPath -> gmPath.endsWith( Properties.DEFAULT_XML_FILE_NAME ) )
.map( gmPath -> gmPath.getParent().toString() ).collect( Collectors.toList() );
}
}

private static Persistence<SutState> createSutStatePersistence() {
return new PersistenceFactory( new HashSet<>( Arrays.asList( StdXmlClassesProvider.getXmlDataClasses() ) ) )
.getPersistence();
}

boolean isDisplayHelp() {
return displayHelp;
}

}

0 comments on commit 9585190

Please sign in to comment.