Github: code review fixes

This commit is contained in:
Aleksey Pivovarov
2013-07-03 19:01:27 +04:00
parent 70f2888a89
commit 34072a4149
10 changed files with 61 additions and 149 deletions
@@ -29,7 +29,7 @@ public class GithubCreateGistTest extends GithubCreateGistTestBase {
public void testSimple() throws Throwable {
List<NamedContent> expected = createContent();
String url = GithubCreateGistAction.createGist(myProject, GithubUtil.getAuthData(), expected, true, "description", null);
String url = GithubCreateGistAction.createGist(myProject, myGitHubSettings.getAuthData(), expected, true, "description", null);
assertNotNull(url);
GIST_ID = url.substring(url.lastIndexOf('/') + 1);
@@ -57,7 +57,7 @@ public class GithubCreateGistTest extends GithubCreateGistTestBase {
public void testUnusedFilenameField() throws Throwable {
List<NamedContent> expected = createContent();
String url = GithubCreateGistAction.createGist(myProject, GithubUtil.getAuthData(), expected, true, "description", "filename");
String url = GithubCreateGistAction.createGist(myProject, myGitHubSettings.getAuthData(), expected, true, "description", "filename");
assertNotNull(url);
GIST_ID = url.substring(url.lastIndexOf('/') + 1);
@@ -72,7 +72,7 @@ public class GithubCreateGistTest extends GithubCreateGistTestBase {
List<NamedContent> content = Collections.singletonList(new NamedContent("file.txt", "file.txt content"));
List<NamedContent> expected = Collections.singletonList(new NamedContent("filename", "file.txt content"));
String url = GithubCreateGistAction.createGist(myProject, GithubUtil.getAuthData(), content, true, "description", "filename");
String url = GithubCreateGistAction.createGist(myProject, myGitHubSettings.getAuthData(), content, true, "description", "filename");
assertNotNull(url);
GIST_ID = url.substring(url.lastIndexOf('/') + 1);
@@ -86,7 +86,7 @@ public class GithubCreateGistTest extends GithubCreateGistTestBase {
public void testPublic() throws Throwable {
List<NamedContent> expected = createContent();
String url = GithubCreateGistAction.createGist(myProject, GithubUtil.getAuthData(), expected, false, "description", null);
String url = GithubCreateGistAction.createGist(myProject, myGitHubSettings.getAuthData(), expected, false, "description", null);
assertNotNull(url);
GIST_ID = url.substring(url.lastIndexOf('/') + 1);
@@ -100,7 +100,7 @@ public class GithubCreateGistTest extends GithubCreateGistTestBase {
public void testEmpty() throws Throwable {
List<NamedContent> expected = Collections.emptyList();
String url = GithubCreateGistAction.createGist(myProject, GithubUtil.getAuthData(), expected, true, "description", null);
String url = GithubCreateGistAction.createGist(myProject, myGitHubSettings.getAuthData(), expected, true, "description", null);
assertNull("Gist was created", url);
checkNotification(NotificationType.WARNING, "Can't create Gist", "Can't create empty gist");
@@ -109,7 +109,7 @@ public class GithubCreateGistTest extends GithubCreateGistTestBase {
public void testWrongLogin() throws Throwable {
List<NamedContent> expected = createContent();
GithubAuthData auth = GithubUtil.getAuthData();
GithubAuthData auth = myGitHubSettings.getAuthData();
GithubAuthData myAuth = new GithubAuthData(auth.getHost(), auth.getLogin() + "some_suffix", auth.getPassword());
String url = GithubCreateGistAction.createGist(myProject, myAuth, expected, true, "description", null);
assertNull("Gist was created", url);
@@ -120,7 +120,7 @@ public class GithubCreateGistTest extends GithubCreateGistTestBase {
public void testWrongPassword() throws Throwable {
List<NamedContent> expected = createContent();
GithubAuthData auth = GithubUtil.getAuthData();
GithubAuthData auth = myGitHubSettings.getAuthData();
GithubAuthData myAuth = new GithubAuthData(auth.getHost(), auth.getLogin(), auth.getPassword() + "some_suffix");
String url = GithubCreateGistAction.createGist(myProject, myAuth, expected, true, "description", null);
assertNull("Gist was created", url);
@@ -29,8 +29,8 @@ import static org.jetbrains.plugins.github.GithubCreateGistAction.NamedContent;
* @author Aleksey Pivovarov
*/
public abstract class GithubCreateGistTestBase extends GithubTest {
protected static String GIST_ID = null;
protected static JsonObject GIST = null;
protected String GIST_ID = null;
protected JsonObject GIST = null;
@Override
public void setUp() throws Exception {
@@ -43,14 +43,9 @@ public abstract class GithubCreateGistTestBase extends GithubTest {
super.tearDown();
}
protected void deleteGist() {
protected void deleteGist() throws IOException {
if (GIST_ID != null) {
try {
GithubUtil.deleteGist(GithubUtil.getAuthData(), GIST_ID);
}
catch (IOException e) {
System.err.println(e.getMessage());
}
GithubUtil.deleteGist(myGitHubSettings.getAuthData(), GIST_ID);
GIST = null;
GIST_ID = null;
}
@@ -68,12 +63,12 @@ public abstract class GithubCreateGistTestBase extends GithubTest {
}
@NotNull
protected static JsonObject getGist() {
protected JsonObject getGist() {
assertNotNull(GIST_ID);
if (GIST == null) {
try {
GIST = GithubUtil.getGist(GithubUtil.getAuthData(), GIST_ID);
GIST = GithubUtil.getGist(myGitHubSettings.getAuthData(), GIST_ID);
}
catch (IOException e) {
System.err.println(e.getMessage());
@@ -84,41 +79,41 @@ public abstract class GithubCreateGistTestBase extends GithubTest {
return GIST;
}
protected static void checkGistExists() {
protected void checkGistExists() {
getGist();
}
protected static void checkGistPublic() {
protected void checkGistPublic() {
JsonObject result = getGist();
assertTrue("Gist does not public", result.get("public").getAsBoolean());
}
protected static void checkGistPrivate() {
protected void checkGistPrivate() {
JsonObject result = getGist();
assertFalse("Gist does not private", result.get("public").getAsBoolean());
}
protected static void checkGistAnonymous() {
protected void checkGistAnonymous() {
JsonObject result = getGist();
assertTrue("Gist does not anonymous", result.get("user").isJsonNull());
}
protected static void checkGistNotAnonymous() {
protected void checkGistNotAnonymous() {
JsonObject result = getGist();
assertFalse("Gist does not anonymous", result.get("user").isJsonNull());
}
protected static void checkGistDescription(@NotNull String expected) {
protected void checkGistDescription(@NotNull String expected) {
JsonObject result = getGist();
assertEquals("Gist content differs from sample", expected, result.get("description").getAsString());
}
protected static void checkGistContent(@NotNull List<NamedContent> expected) {
protected void checkGistContent(@NotNull List<NamedContent> expected) {
JsonObject result = getGist();
JsonObject files = result.get("files").getAsJsonObject();
@@ -129,7 +124,7 @@ public abstract class GithubCreateGistTestBase extends GithubTest {
}
}
protected static void checkEquals(@NotNull List<NamedContent> expected, @NotNull List<NamedContent> actual) {
protected void checkEquals(@NotNull List<NamedContent> expected, @NotNull List<NamedContent> actual) {
for (NamedContent file : expected) {
assertTrue("Not found: <" + file.getName() + "> " + file.getText(), actual.contains(file));
}
@@ -25,10 +25,10 @@ import java.util.List;
*/
public class GithubRequestPagingTest extends GithubTest {
public void testCase() throws Throwable {
public void testAvailableRepos() throws Throwable {
GithubSettings.getInstance().setAuthData(auth2, false);
List<RepositoryInfo> availableRepos = GithubUtil.getAvailableRepos(GithubUtil.getAuthData());
List<RepositoryInfo> availableRepos = GithubUtil.getAvailableRepos(myGitHubSettings.getAuthData());
List<String> realData = new ArrayList<String>();
for (RepositoryInfo info : availableRepos) {
realData.add(info.getName());
@@ -15,19 +15,12 @@
*/
package org.jetbrains.plugins.github;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.ui.DialogWrapper;
import git4idea.GitUtil;
import git4idea.Notificator;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import git4idea.test.GitExecutor;
import git4idea.test.TestDialogHandler;
import git4idea.test.TestNotificator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.github.test.GithubTest;
import org.jetbrains.plugins.github.ui.GithubShareDialog;
@@ -38,12 +31,14 @@ import java.util.Random;
* @author Aleksey Pivovarov
*/
public abstract class GithubShareProjectTestBase extends GithubTest {
protected static String PROJECT_NAME;
protected String PROJECT_NAME;
protected GitRepositoryManager myGitRepositoryManager;
@Override
public void setUp() throws Exception {
super.setUp();
myGitRepositoryManager = GitUtil.getRepositoryManager(myProject);
Random rnd = new Random();
PROJECT_NAME = "new_project_from_share_test_" + rnd.nextLong();
registerHttpAuthService();
@@ -55,13 +50,8 @@ public abstract class GithubShareProjectTestBase extends GithubTest {
super.tearDown();
}
protected void deleteGithubRepo() {
try {
GithubUtil.deleteGithubRepository(GithubUtil.getAuthData(), PROJECT_NAME);
}
catch (IOException e) {
System.err.println(e.getMessage());
}
protected void deleteGithubRepo() throws IOException {
GithubUtil.deleteGithubRepository(myGitHubSettings.getAuthData(), PROJECT_NAME);
}
protected void registerDefaultShareDialogHandler() {
@@ -85,27 +75,25 @@ public abstract class GithubShareProjectTestBase extends GithubTest {
}
protected void checkGithubExists() {
RepositoryInfo githubInfo = GithubUtil.getDetailedRepoInfo(GithubUtil.getAuthData(), GithubUtil.getAuthData().getLogin(), PROJECT_NAME);
GithubAuthData auth = myGitHubSettings.getAuthData();
RepositoryInfo githubInfo = GithubUtil.getDetailedRepoInfo(auth, auth.getLogin(), PROJECT_NAME);
assertNotNull("Github repository does not exist", githubInfo);
}
protected void checkGitExists() {
final GitRepositoryManager manager = GitUtil.getRepositoryManager(myProject);
final GitRepository gitRepository = manager.getRepositoryForFile(myProjectRoot);
final GitRepository gitRepository = myGitRepositoryManager.getRepositoryForFile(myProjectRoot);
assertNotNull("Git repository does not exist", gitRepository);
}
protected void checkRemoteConfigured() {
final GitRepositoryManager manager = GitUtil.getRepositoryManager(myProject);
final GitRepository gitRepository = manager.getRepositoryForFile(myProjectRoot);
final GitRepository gitRepository = myGitRepositoryManager.getRepositoryForFile(myProjectRoot);
assertNotNull(gitRepository);
assertNotNull("Github remote does not configured", GithubUtil.findGithubRemoteUrl(gitRepository));
}
protected void checkLastCommitPushed() {
final GitRepositoryManager manager = GitUtil.getRepositoryManager(myProject);
final GitRepository gitRepository = manager.getRepositoryForFile(myProjectRoot);
final GitRepository gitRepository = myGitRepositoryManager.getRepositoryForFile(myProjectRoot);
assertNotNull(gitRepository);
String hash = GitExecutor.git(gitRepository, "log -1 --pretty=%h");
@@ -17,14 +17,12 @@ package org.jetbrains.plugins.github.test;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.PlatformTestCase;
import com.intellij.testFramework.UsefulTestCase;
import com.intellij.testFramework.VfsTestUtil;
import com.intellij.testFramework.fixtures.IdeaProjectTestFixture;
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory;
import git4idea.DialogManager;
@@ -42,8 +40,6 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.github.GithubAuthData;
import org.jetbrains.plugins.github.GithubSettings;
import java.io.IOException;
import static org.junit.Assume.assumeNotNull;
/**
@@ -83,77 +79,13 @@ public abstract class GithubTest extends UsefulTestCase {
}
protected void createProjectFiles() {
createFile("file.txt");
createFile("file");
createFile("folder/file1");
createFile("folder/file2");
createFile("folder/empty_file", "");
createFile("folder/empty_folder/");
createFile("folder/dir/file3");
}
protected void createFile(@NotNull String path) {
createFile(path, path.substring(path.lastIndexOf('/') + 1) + " content");
}
protected void createFile(@NotNull String path, @NotNull String content) {
String[] pathElements = path.split("/");
boolean lastIsDir = path.endsWith("/");
VirtualFile currentParent = myProjectRoot;
for (int i = 0; i < pathElements.length - 1; i++) {
currentParent = createDir(myProject, currentParent, pathElements[i]);
}
String lastElement = pathElements[pathElements.length - 1];
if (lastIsDir) {
createDir(myProject, currentParent, lastElement);
}
else {
createFile(myProject, currentParent, lastElement, content);
}
}
protected static VirtualFile createFile(@NotNull Project project,
@NotNull final VirtualFile parent,
@NotNull final String name,
@Nullable final String content) {
final Ref<VirtualFile> result = new Ref<VirtualFile>();
new WriteCommandAction.Simple(project) {
@Override
protected void run() throws Throwable {
try {
VirtualFile file = parent.createChildData(this, name);
if (content != null) {
file.setBinaryContent(CharsetToolkit.getUtf8Bytes(content));
}
result.set(file);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
}.execute();
return result.get();
}
protected static VirtualFile createDir(@NotNull Project project, @NotNull final VirtualFile parent, @NotNull final String name) {
final Ref<VirtualFile> result = new Ref<VirtualFile>();
new WriteCommandAction.Simple(project) {
@Override
protected void run() throws Throwable {
try {
VirtualFile dir = parent.findChild(name);
if (dir == null) {
dir = parent.createChildDirectory(this, name);
}
result.set(dir);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
}.execute();
return result.get();
VfsTestUtil.createFile(myProjectRoot, "file.txt", "file.txt content");
VfsTestUtil.createFile(myProjectRoot, "file", "file content");
VfsTestUtil.createFile(myProjectRoot, "folder/file1", "file1 content");
VfsTestUtil.createFile(myProjectRoot, "folder/file2", "file2 content");
VfsTestUtil.createFile(myProjectRoot, "folder/empty_file");
VfsTestUtil.createFile(myProjectRoot, "folder/dir/file3", "file3 content");
VfsTestUtil.createDir (myProjectRoot, "folder/empty_folder");
}
protected void checkNotification(@NotNull NotificationType type, @Nullable String title, @Nullable String content) {
@@ -151,7 +151,7 @@ public class GithubApiUtil {
case HEAD:
return new HeadMethod(uri);
default:
throw new IllegalStateException("Wrong HttpVerb: unknown method");
throw new IllegalStateException("Wrong HttpVerb: unknown method: " + verb.toString());
}
}
});
@@ -198,4 +198,9 @@ public class GithubSettings implements PersistentStateComponent<Element> {
setLogin(auth.getLogin());
setPassword(auth.getPassword(), rememberPassword);
}
@NotNull
public GithubAuthData getAuthData() {
return new GithubAuthData(getHost(), getLogin(), getPassword());
}
}
@@ -44,6 +44,7 @@ abstract class GithubShowCommitInBrowserAction extends DumbAwareAction {
if (userAndRepository == null) {
GithubNotifications
.showError(project, GithubOpenInBrowserAction.CANNOT_OPEN_IN_BROWSER, "Cannot extract info about repository: " + url);
return;
}
String githubUrl = GithubApiUtil.getGitHost() + "/" + userAndRepository + "/commit/" + revisionHash;
@@ -54,7 +54,7 @@ public class GithubUtil {
public static GithubAuthData runAndGetValidAuth(@NotNull Project project,
@NotNull ProgressIndicator indicator,
@NotNull ThrowableConsumer<GithubAuthData, IOException> task) throws IOException {
GithubAuthData auth = getAuthData();
GithubAuthData auth = GithubSettings.getInstance().getAuthData();
try {
task.consume(auth);
return auth;
@@ -81,12 +81,6 @@ public class GithubUtil {
}
}
@NotNull
public static GithubAuthData getAuthData() {
GithubSettings settings = GithubSettings.getInstance();
return new GithubAuthData(settings.getHost(), settings.getLogin(), settings.getPassword());
}
@Nullable
public static GithubAuthData getValidAuthData(@NotNull Project project, @NotNull ProgressIndicator indicator) {
final GithubLoginDialog dialog = new GithubLoginDialog(project);
@@ -104,13 +98,13 @@ public class GithubUtil {
@Nullable
public static GithubAuthData getValidAuthDataFromConfig(@NotNull Project project, @NotNull ProgressIndicator indicator) {
GithubAuthData auth = getAuthData();
GithubAuthData auth = GithubSettings.getInstance().getAuthData();
boolean valid = false;
try {
valid = checkAuthData(auth);
}
catch (IOException e) {
// ignore
LOG.error("Connection error", e);
}
if (!valid) {
return getValidAuthData(project, indicator);
@@ -303,17 +297,18 @@ public class GithubUtil {
*/
@Nullable
public static String getUserAndRepositoryFromRemoteUrl(@NotNull String remoteUrl) {
String url = removeEndingDotGit(remoteUrl);
remoteUrl = removeEndingDotGit(remoteUrl);
int index;
index = url.lastIndexOf('/');
index = remoteUrl.lastIndexOf('/');
if (index == -1) {
return null;
}
index = url.substring(0, index).lastIndexOf('/');
String url = remoteUrl.substring(0, index);
index = Math.max(url.lastIndexOf('/'), url.lastIndexOf(':'));
if (index == -1) {
return null;
}
return url.substring(index + 1);
return remoteUrl.substring(index + 1);
}
/**
@@ -40,17 +40,13 @@ public class GitHubCreateGistDialog extends DialogWrapper {
myGithubCreateGistPanel.setAnonymous(settings.isAnonymous());
myGithubCreateGistPanel.setPrivate(settings.isPrivateGist());
myGithubCreateGistPanel.setOpenInBrowser(settings.isOpenInBrowserGist());
if (editor != null) {
if (file != null) {
myGithubCreateGistPanel.showFileNameField(file.getName());
}
else {
myGithubCreateGistPanel.showFileNameField("");
}
}
else if (file != null && !file.isDirectory()) {
if (file != null && !file.isDirectory()) {
myGithubCreateGistPanel.showFileNameField(file.getName());
}
else if (editor != null) {
myGithubCreateGistPanel.showFileNameField("");
}
setTitle("Create Gist");
init();
}