Skip to content
This repository has been archived by the owner on Nov 11, 2022. It is now read-only.

Commit

Permalink
Add tests for active gcloud config
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafal Wojdyla committed Aug 19, 2016
1 parent 62db4af commit 4261b7b
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,76 @@ public void testGetProjectFromUserHomeOldAndNewPrefersNew() throws Exception {
environment));
}

@Test
public void testGetProjectFromUserHomeDefaultAndActivePrefersActive() throws Exception {
Map<String, String> environment = ImmutableMap.of();
System.setProperty("user.home", tmpFolder.getRoot().getAbsolutePath());

makeActiveConfigFile(new File(tmpFolder.newFolder(".config", "gcloud"), "active_config"),
"testactive"); // active configuration name

File configurations = tmpFolder.newFolder(".config", "gcloud", "configurations");
makePropertiesFileWithProject(
new File(configurations, "config_testactive"),
"active-project"); // name of the active project

assertEquals("active-project",
runGetProjectTest(
new File(configurations, "config_default"),
environment));
}

@Test
public void testGetProjectFromUserHomeDefaultAndEmptyActivePrefersDefault() throws Exception {
Map<String, String> environment = ImmutableMap.of();
System.setProperty("user.home", tmpFolder.getRoot().getAbsolutePath());

makeActiveConfigFile(new File(tmpFolder.newFolder(".config", "gcloud"), "active_config"),
""); // active configuration name

File configurations = tmpFolder.newFolder(".config", "gcloud", "configurations");
makePropertiesFileWithProject(
new File(configurations, "config_testactive"),
"active-project"); // name of the active project

assertEquals("test-project",
runGetProjectTest(
new File(configurations, "config_default"),
environment));
}

@Test
public void testGetProjectFromUserHomeDefaultAndInvalidActivePrefersDefault() throws Exception {
Map<String, String> environment = ImmutableMap.of();
System.setProperty("user.home", tmpFolder.getRoot().getAbsolutePath());

makeActiveConfigFile(new File(tmpFolder.newFolder(".config", "gcloud"), "active_config"),
"testactive\ntestactive2"); // active configuration name

File configurations = tmpFolder.newFolder(".config", "gcloud", "configurations");
makePropertiesFileWithProject(
new File(configurations, "config_testactive"),
"active-project"); // name of the active project

assertEquals("test-project",
runGetProjectTest(
new File(configurations, "config_default"),
environment));
}

@Test
public void testGetProjectFromUserHomeOldAndNoActiveFilePrefersOld() throws Exception {
Map<String, String> environment = ImmutableMap.of();
System.setProperty("user.home", tmpFolder.getRoot().getAbsolutePath());

File gcloudFolder = tmpFolder.newFolder(".config", "gcloud");
makeActiveConfigFile(new File(gcloudFolder, "active_config"), "testactive");
assertEquals("test-project",
runGetProjectTest(
new File(gcloudFolder, "properties"),
environment));
}

@Test
public void testUnableToGetDefaultProject() throws Exception {
System.setProperty("user.home", tmpFolder.getRoot().getAbsolutePath());
Expand All @@ -101,6 +171,11 @@ public void testUnableToGetDefaultProject() throws Exception {
assertNull(projectFactory.create(PipelineOptionsFactory.create()));
}

private static void makeActiveConfigFile(File path, String activeConfigName)
throws IOException {
Files.write(activeConfigName, path, StandardCharsets.UTF_8);
}

private static void makePropertiesFileWithProject(File path, String projectId)
throws IOException {
String properties = String.format("[core]%n"
Expand Down

0 comments on commit 4261b7b

Please sign in to comment.