/*
 * Copyright 2019 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.template.soy;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.common.base.Joiner;
import java.io.File;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.kohsuke.args4j.CmdLineParser;

@RunWith(JUnit4.class)
public final class PerInputOutputFilesTest {
  final PerInputOutputFiles outputFiles = new PerInputOutputFiles("soy.js", Joiner.on("\n"));

  @Test
  public void testValidate_setBothSets() throws Exception {
    new CmdLineParser(outputFiles)
        .parseArgument(
            new String[] {"--outputPathFormat", "foo/bar.js", "--outputDirectory", "foo/"});
    try {
      outputFiles.validateFlags();
      fail();
    } catch (CommandLineError cme) {
      assertThat(cme)
          .hasMessageThat()
          .isEqualTo(
              "Error: Must set either --outputPathFormat or --outputDirectory and --inputRoots.");
    }
  }

  @Test
  public void testValidate_setNeither() throws Exception {
    new CmdLineParser(outputFiles).parseArgument(new String[] {});
    try {
      outputFiles.validateFlags();
      fail();
    } catch (CommandLineError cme) {
      assertThat(cme)
          .hasMessageThat()
          .isEqualTo("Error: Must set at least one of --outputPathFormat or --outputDirectory.");
    }
  }

  @Test
  public void testGetOutputPath_inputRoots() throws Exception {
    new CmdLineParser(outputFiles)
        .parseArgument(new String[] {"--outputDirectory", "out", "--inputRoots", "root"});
    assertThat(outputFiles.getOutputPath(new File("root/foo/bar.soy"), /*locale=*/ null).toString())
        .isEqualTo("out/foo/bar.soy.js");
    // files not under a root are copied as is
    assertThat(outputFiles.getOutputPath(new File("src/foo/bar.soy"), /*locale=*/ null).toString())
        .isEqualTo("out/src/foo/bar.soy.js");
  }

  // just a basic test, there are more tests in MainEntryPointUtilsTest
  @Test
  public void testGetOutputPath_outputPathFormat() throws Exception {
    new CmdLineParser(outputFiles).parseArgument(new String[] {"--outputPathFormat", "foo.js"});
    assertThat(outputFiles.getOutputPath(new File("root/foo/bar.soy"), /*locale=*/ null).toString())
        .isEqualTo("foo.js");
  }
}

@@ -775,16 +775,41 @@
 o.File;%0A
+import org.junit.Before;%0A
 import o
@@ -892,49 +892,8 @@
 it4;
-%0Aimport org.kohsuke.args4j.CmdLineParser;
 %0A%0A@R
@@ -960,16 +960,24 @@
 est %7B%0A  
+private 
 final Pe
@@ -1007,16 +1007,22 @@
 tFiles =
+%0A     
  new Per
@@ -1067,16 +1067,226 @@
 %5Cn%22));%0A%0A
+  private SoyCmdLineParser cmdLineParser;%0A%0A  @Before%0A  public final void constructParser() %7B%0A    cmdLineParser = new SoyCmdLineParser(/*loader=*/ null);%0A    cmdLineParser.registerFlagsObject(outputFiles);%0A  %7D%0A%0A
   @Test%0A
@@ -1341,37 +1341,33 @@
 Exception %7B%0A    
-new C
+c
 mdLineParser(out
@@ -1366,72 +1366,23 @@
 rser
-(outputFiles)%0A        .parseArgument(%0A            new String%5B%5D %7B
+.parseArgument(
 %22--o
@@ -1440,17 +1440,16 @@
 , %22foo/%22
-%7D
 );%0A    t
@@ -1784,37 +1784,33 @@
 Exception %7B%0A    
-new C
+c
 mdLineParser(out
@@ -1801,37 +1801,24 @@
 mdLineParser
-(outputFiles)
 .parseArgume
@@ -1824,23 +1824,8 @@
 ent(
-new String%5B%5D %7B%7D
 );%0A 
@@ -2146,37 +2146,33 @@
 Exception %7B%0A    
-new C
+c
 mdLineParser(out
@@ -2171,30 +2171,8 @@
 rser
-(outputFiles)%0A        
 .par
@@ -2178,38 +2178,24 @@
 rseArgument(
-new String%5B%5D %7B
 %22--outputDir
@@ -2232,17 +2232,16 @@
 , %22root%22
-%7D
 );%0A    a
@@ -2734,13 +2734,9 @@
     
-new C
+c
 mdLi
@@ -2743,29 +2743,16 @@
 neParser
-(outputFiles)
 .parseAr
@@ -2762,22 +2762,8 @@
 ent(
-new String%5B%5D %7B
 %22--o
@@ -2788,17 +2788,16 @@
 %22foo.js%22
-%7D
 );%0A    a

Fix flaky //third_party/java_src/soy/opensource:verify_open_source_build_test