/*
 * Copyright (C) 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.copybara;

import static com.google.common.truth.Truth.assertThat;

import com.beust.jcommander.JCommander;
import com.google.common.collect.ImmutableMap;
import com.google.copybara.util.ExitCode;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Test to verify that framework methods are called as expected.
 */
@RunWith(JUnit4.class)
public class MainTest {

  private String[] args = {"copy.bara.sky"};
  boolean called = false;

  @Rule
  public final ExpectedException thrown = ExpectedException.none();

  @Before
  public void setUp() throws Exception {
    called = false;
    System.setProperty("user.home", "/tmp/foo/bar");
  }

  @Test
  public void testNoArguments() {
    assertThat(new Main().run(/*no arguments*/ new String[]{}))
        .isEqualTo(ExitCode.COMMAND_LINE_ERROR);
  }

  @Test
  public void testConfigureLogCalled() throws IOException {

    ImmutableMap<String, String> envWithHome = ImmutableMap.of("HOME",
        Files.createTempDirectory("foo").toString());
    Main main = new Main(envWithHome) {
          @Override
          protected void configureLog(FileSystem fs) {
            called = true;
          }
        };
    assertThat(main.run(args)).isEqualTo(ExitCode.COMMAND_LINE_ERROR);
    assertThat(called).isTrue();
  }

  @Test
  public void testInitEnvironmentCalled() {
    Main main =
        new Main() {
          @Override
          protected void configureLog(FileSystem fs) {}

          @Override
          protected void initEnvironment(Options o, MainArguments args, JCommander jcommander) {
            called = true;
          }
        };
    main.run(args);
    assertThat(called).isTrue();
  }

  @Test
  public void testShutdownCalled() {
    Main main =
        new Main() {
          @Override
          protected void configureLog(FileSystem fs) {}

          @Override
          protected void shutdown(ExitCode exitCode) {
            called = true;
          }
        };
    main.run(args);
    assertThat(called).isTrue();
  }
}

@@ -894,16 +894,43 @@
 .Files;%0A
+import java.nio.file.Path;%0A
 import o
@@ -1369,16 +1369,44 @@
 .none();
+%0A  private Path userHomeDir;
 %0A%0A  @Bef
@@ -1470,16 +1470,73 @@
  false;%0A
+    userHomeDir = Files.createTempDirectory(%22MainTest%22);%0A
     Syst
@@ -1567,22 +1567,30 @@
 e%22, 
-%22/tmp/foo/bar%22
+userHomeDir.toString()
 );%0A 

Use a temporary directory instead of a hardcoded path