Skip to content

Commit

Permalink
Merge pull request #227 from xshyamx/fix/empty-dir-init
Browse files Browse the repository at this point in the history
Fixes #225: init/info fail when non-existent base path is passed
  • Loading branch information
harawata committed Jul 3, 2022
2 parents 8a9fe60 + 74c7fde commit 2c55480
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/main/java/org/apache/ibatis/migration/CommandLine.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 the original author or authors.
* Copyright 2010-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import java.util.Date;

import org.apache.ibatis.migration.commands.Command;
import org.apache.ibatis.migration.commands.Commands;
import org.apache.ibatis.migration.options.Options;
import org.apache.ibatis.migration.options.SelectedOptions;
import org.apache.ibatis.migration.utils.Util;
Expand All @@ -38,7 +39,8 @@ public CommandLine(String[] args) {
public void execute() {
final SelectedOptions selectedOptions = parse(args);
try {
if (!validOptions(selectedOptions) || selectedOptions.needsHelp()) {
// order is important as if !needsHelp then a valid command is required but, not vice-versa
if (selectedOptions.needsHelp() || !validOptions(selectedOptions)) {
printUsage();
} else {
runCommand(selectedOptions);
Expand Down Expand Up @@ -114,8 +116,9 @@ private boolean validOptions(SelectedOptions selectedOptions) {
console.printf("No command specified.%n");
return false;
}

return validBasePath(selectedOptions.getPaths().getBasePath());
String cmd = selectedOptions.getCommand().toUpperCase();
return Commands.INIT.name().startsWith(cmd) || Commands.INFO.name().startsWith(cmd)
|| validBasePath(selectedOptions.getPaths().getBasePath());
}

private boolean validBasePath(File basePath) {
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/apache/ibatis/migration/MigratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ void shouldInitTempDirectory() throws Exception {
assertEquals(3, scriptPath.list().length);
Migrator.main(TestUtil.args("--path=" + basePath.getAbsolutePath(), "new", "test new migration"));
assertEquals(4, scriptPath.list().length);
assertTrue(TestUtil.deleteDirectory(basePath), "delete temp dir");
}

@Test
Expand All @@ -344,6 +345,7 @@ void shouldRespectIdPattern() throws Exception {
File newMigration = new File(
basePath.getCanonicalPath() + File.separator + "scripts" + File.separator + "003_new_migration.sql");
assertTrue(newMigration.exists());
assertTrue(TestUtil.deleteDirectory(basePath), "delete temp dir");
}

@Test
Expand Down Expand Up @@ -371,6 +373,7 @@ void useCustomTemplate() throws Exception {
}
}
templatePath.delete();
assertTrue(TestUtil.deleteDirectory(basePath), "delete temp dir");
}

@Test
Expand All @@ -387,6 +390,7 @@ void useCustomTemplateWithNoValue() throws Exception {
Migrator.main(TestUtil.args("--path=" + basePath.getAbsolutePath(), "new", "test new migration", "--template="));
assertEquals(4, scriptPath.list().length);
templatePath.delete();
assertTrue(TestUtil.deleteDirectory(basePath), "delete temp dir");
}

@Test
Expand All @@ -405,6 +409,7 @@ void useCustomTemplateWithBadPath() throws Exception {
assertEquals(4, scriptPath.list().length);
assertTrue(output
.contains("Your migrations configuration did not find your custom template. Using the default template."));
assertTrue(TestUtil.deleteDirectory(basePath), "delete temp dir");
}

@Test
Expand All @@ -416,6 +421,7 @@ void shouldSuppressOutputIfQuietOptionEnabled() throws Throwable {
});
assertFalse(output.contains("Initializing:"));
assertNotNull(basePath.list());
assertTrue(TestUtil.deleteDirectory(basePath), "delete temp dir");
}

@Test
Expand All @@ -427,6 +433,7 @@ void shouldColorizeSuccessOutputIfColorOptionEnabled() throws Throwable {
});
assertTrue(output.contains(ConsoleColors.GREEN + "SUCCESS"));
assertNotNull(basePath.list());
assertTrue(TestUtil.deleteDirectory(basePath), "delete temp dir");
}

@Test
Expand All @@ -440,6 +447,7 @@ void shouldColorizeFailureOutputIfColorOptionEnabled() throws Throwable {
assertEquals(1, exitCode);
});
assertTrue(output.contains(ConsoleColors.RED + "FAILURE"));
assertTrue(TestUtil.deleteDirectory(basePath), "delete temp dir");
}

@Test
Expand Down Expand Up @@ -474,4 +482,29 @@ void testInfoCommand() throws Exception {
assertFalse(output.contains("null"), output);
}

@Test
void testInfoWithNonExistentBasePath() throws Exception {
File baseDir = TestUtil.getTempDir();
assertTrue(baseDir.delete()); // remove empty dir
assertFalse(baseDir.exists(), "directory does not exist");
String output = SystemLambda.tapSystemOut(() -> {
Migrator.main(TestUtil.args("info", "--path=" + baseDir.getAbsolutePath()));
});
assertFalse(output.contains("Migrations path must be a directory"), "base path not required for info");
assertFalse(output.contains("null"), output);
}

@Test
void testInitWithNonExistentBasePath() throws Exception {
File baseDir = TestUtil.getTempDir();
assertTrue(baseDir.delete()); // remove empty dir
assertFalse(baseDir.exists(), "directory does not exist");
String output = SystemLambda
.tapSystemOut(() -> Migrator.main(TestUtil.args("init", "--path=" + baseDir.getAbsolutePath())));
assertFalse(output.contains("Migrations path must be a directory"), output);
assertTrue(new File(baseDir, "README").exists(), "README created");
assertTrue(new File(baseDir, "environments").isDirectory(), "environments directory created");
assertTrue(TestUtil.deleteDirectory(baseDir), "delete temp dir");
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 the original author or authors.
* Copyright 2010-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,6 +51,7 @@ void shouldRunNewHooks() throws Throwable {
assertTrue(output.contains("Description is valid."));
assertTrue(output.contains("Renamed 03_create_table1_JIRA-123.sql to 03_create_table1_JIRA123.sql"));
assertTrue(new File(scriptPath, "03_create_table1_JIRA123.sql").exists());
assertTrue(TestUtil.deleteDirectory(basePath), "delete test dir");
}

@Test
Expand All @@ -65,6 +66,7 @@ void shouldNotCreateFileWhenBeforeHookThrowsException() throws Throwable {
});
assertTrue(output.contains("FAILURE"));
assertEquals(3, scriptPath.list().length);
assertTrue(TestUtil.deleteDirectory(basePath), "delete test dir");
}

protected File initBaseDir() throws IOException {
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/org/apache/ibatis/migration/utils/TestUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 the original author or authors.
* Copyright 2010-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,4 +58,14 @@ public static File getTempDir() throws IOException {
f.deleteOnExit();
return f;
}

public static boolean deleteDirectory(File dir) throws IOException {
boolean result = dir.exists();
if (result) {
for (File f : dir.listFiles()) {
result = result && (f.isDirectory() ? deleteDirectory(f) : f.delete());
}
}
return result && dir.delete();
}
}

0 comments on commit 2c55480

Please sign in to comment.