package jetbrains.mps.generator.xmodel.build.test.build;
/*Generated by MPS */
import junit.framework.TestCase;
import com.intellij.execution.process.ProcessAdapter;
import com.intellij.execution.process.ProcessEvent;
import com.intellij.openapi.util.Key;
import com.intellij.execution.process.ProcessOutputTypes;
import com.intellij.execution.process.ProcessHandler;
import jetbrains.mps.ant.execution.Ant_Command;
import jetbrains.mps.execution.api.commands.OutputRedirector;
import jetbrains.mps.execution.api.commands.ProcessHandlerBuilder;
import junit.framework.Assert;
import jetbrains.mps.tool.environment.EnvironmentConfig;
import jetbrains.mps.tool.environment.IdeaEnvironment;
/**
* Test layout:
* mps/languages/languageDesign/generator/test/xmodel.build
* Location of this test module. In addition to test class, the module include 2 build projects to run
* during tests. Modules built by these projects are not part of "MPS" project itself, and live in a separate project,
* "project.xmodel.test1".
* This location is also home for p1.xml and p2.xml, as these use "../.." as their base directory to get
* to generator/test/../../project.xmodel.test1/ home of test modules.
* mps/languages/languageDesign/generator/project.xmodel.test1
* Location of solutions, languages and devkits we use during tests. Namely, p1 and p2 build projects from this module
* build these modules
*
* p1 and p2 generate their respective xmls relative to basedir, hence use test/xmodel.build/name.xml to get them into proper place.
* Basedir is set to ../../, not individual modules are loaded from ../../ as Build Language for whatever reason doesn't
* tolerate modules not under project's basedir.
*
* Test process:
* First, we generate set of modules (language, devkit and solution with x-ref target) with p1.xml.
* Then, we need to assemble/deploy these modules so that p2 build process can use them.
* Last, we generate second module, with x-ref source, using p2.xml. Cross-model references are resolved if generation completes without an error.
*
* TO RUN FROM MPS SOURCES: in Run Configuration, set Working directory to mps project root folder, extract an MPS distribution
* and pass its location with mps_distribution VM argument (i.e. -Dmps_distribution=/Downloads/MPS-171.333/)
* To run the test from MPS sources, one may need to specify Working directory in the JUnit run configuration to point to mps root
* (I've seen mps/bin as default home folder). Ant starts p1.xml and p2.xml scripts using location relative to mps project root.
* IMPORTANT: p1 and p2 explicitly list 'java' among used plugins and reference artifacts of 'mps' dependency through custom mps_distribution macro (not mps_home)
* as it's the only chance to get ant-mps.jar location relavive to {artifacts.mps}/, not {mps_home}.
* It's vital for running tests from MPS sources as ant() command implicitly set mps_home to that of running MPS instance, which
* doesn't have ant-mps.jar where build script expects it (/lib/ant/lib/ant-mps.jar). Since we can't override mps_home from the test, we
* use a trick to supply mps_distribution that points to an unzipped MPS distribution (from MPS-version.zip)
*/
public class TwoModulesWithXRefsBuiltIndependently_Test extends TestCase {
public void test_build2projects() throws Exception {
try {
final ProcessAdapter textOutputAdapter = new ProcessAdapter() {
@Override
public void onTextAvailable(ProcessEvent event, Key key) {
if (ProcessOutputTypes.STDERR.equals(key)) {
// print errors
System.err.print(event.getText());
} else {
System.out.print(event.getText());
}
}
};
String mpsDistrOption = null;
if (System.getProperty("mps_distribution") != null) {
mpsDistrOption = "-Dmps_distribution=" + System.getProperty("mps_distribution");
}
ProcessHandler process1 = new Ant_Command().setOptions_String(mpsDistrOption).setTargetName_String("generate assemble").createProcess("testbench/modules/xmodel.build/p1.xml");
OutputRedirector.redirect(process1, textOutputAdapter);
int exitCode = ProcessHandlerBuilder.startAndWait(process1);
if (exitCode != 0) {
Assert.fail("Exited with code " + exitCode);
}
ProcessHandler process2 = new Ant_Command().setOptions_String(mpsDistrOption).setTargetName_String("generate").createProcess("testbench/modules/xmodel.build/p2.xml");
OutputRedirector.redirect(process2, textOutputAdapter);
exitCode = ProcessHandlerBuilder.startAndWait(process2);
if (exitCode != 0) {
Assert.fail("Exited with code " + exitCode);
}
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail("Exception during execution.");
}
}
public void setUp() {
EnvironmentConfig environmentConfig = EnvironmentConfig.defaultConfig();
// Need IdeaEnvironment here because ant command uses IDEA's PathMacros, not that of MPS core.
IdeaEnvironment.getOrCreate(environmentConfig);
}
}