Configuration: Don't create kotlinc.xml if the settings don't differ from the defaults

#KT-16647 Fixed
This commit is contained in:
Alexey Sedunov
2017-03-09 15:37:28 +03:00
parent e8749e639c
commit 6b6d7a5030
27 changed files with 515 additions and 244 deletions
@@ -99,6 +99,15 @@ public abstract class CommonCompilerArguments implements Serializable {
public List<String> unknownExtraFlags = new SmartList<String>();
@NotNull
public static CommonCompilerArguments createDefaultInstance() {
DummyImpl arguments = new DummyImpl();
arguments.coroutinesEnable = false;
arguments.coroutinesWarn = true;
arguments.coroutinesError = false;
return arguments;
}
@NotNull
public String executableScriptFileName() {
return "kotlinc";
@@ -71,6 +71,13 @@ public class K2JSCompilerArguments extends CommonCompilerArguments {
@ValueDescription("<path>")
public String outputPostfix;
@NotNull
public static K2JSCompilerArguments createDefaultInstance() {
K2JSCompilerArguments arguments = new K2JSCompilerArguments();
arguments.moduleKind = K2JsArgumentConstants.MODULE_PLAIN;
return arguments;
}
@Override
@NotNull
public String executableScriptFileName() {
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.cli.common.arguments;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.cli.common.parser.com.sampullara.cli.Argument;
import org.jetbrains.kotlin.config.JvmTarget;
public class K2JVMCompilerArguments extends CommonCompilerArguments {
public static final long serialVersionUID = 0L;
@@ -113,10 +114,16 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments {
// Paths to output directories for friend modules.
public String[] friendPaths;
@NotNull
public static K2JVMCompilerArguments createDefaultInstance() {
K2JVMCompilerArguments arguments = new K2JVMCompilerArguments();
arguments.jvmTarget = JvmTarget.DEFAULT.getDescription();
return arguments;
}
@Override
@NotNull
public String executableScriptFileName() {
return "kotlinc-jvm";
}
}
@@ -22,6 +22,9 @@ import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments;
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments;
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
import org.jetbrains.kotlin.config.SettingConstants;
import static com.intellij.openapi.components.StoragePathMacros.PROJECT_CONFIG_DIR;
@@ -29,7 +32,11 @@ import static com.intellij.openapi.components.StoragePathMacros.PROJECT_CONFIG_D
public abstract class BaseKotlinCompilerSettings<T> implements PersistentStateComponent<Element> {
public static final String KOTLIN_COMPILER_SETTINGS_PATH = PROJECT_CONFIG_DIR + "/" + SettingConstants.KOTLIN_COMPILER_SETTINGS_FILE;
private static final SkipDefaultValuesSerializationFilters SKIP_DEFAULT_VALUES = new SkipDefaultValuesSerializationFilters();
private static final SkipDefaultValuesSerializationFilters SKIP_DEFAULT_VALUES = new SkipDefaultValuesSerializationFilters(
CommonCompilerArguments.createDefaultInstance(),
K2JVMCompilerArguments.createDefaultInstance(),
K2JSCompilerArguments.createDefaultInstance()
);
@NotNull
private T settings;
@@ -19,9 +19,13 @@ package org.jetbrains.kotlin.idea.compiler.configuration;
import com.intellij.openapi.components.*;
import com.intellij.openapi.project.Project;
import com.intellij.util.text.VersionComparatorUtil;
import org.jdom.Attribute;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments;
import org.jetbrains.kotlin.config.FacetSerializationKt;
import org.jetbrains.kotlin.config.LanguageVersion;
import static org.jetbrains.kotlin.config.SettingConstants.KOTLIN_COMMON_COMPILER_ARGUMENTS_SECTION;
@@ -33,10 +37,33 @@ import static org.jetbrains.kotlin.config.SettingConstants.KOTLIN_COMMON_COMPILE
}
)
public class KotlinCommonCompilerArgumentsHolder extends BaseKotlinCompilerSettings<CommonCompilerArguments> {
private static String DEFAULT_LANGUAGE_VERSION = LanguageVersion.LATEST.getVersionString();
public static KotlinCommonCompilerArgumentsHolder getInstance(Project project) {
return ServiceManager.getService(project, KotlinCommonCompilerArgumentsHolder.class);
}
private static void dropElementIfDefault(@Nullable Element element) {
if (element == null) return;
Attribute versionAttribute = element.getAttribute("value");
String version = versionAttribute != null ? versionAttribute.getValue() : null;
if (DEFAULT_LANGUAGE_VERSION.equals(version)) {
element.detach();
}
}
@Override
public Element getState() {
Element element = super.getState();
if (element != null) {
// Do not serialize language/api version if they correspond to the default language version
dropElementIfDefault(FacetSerializationKt.getOption(element, "languageVersion"));
dropElementIfDefault(FacetSerializationKt.getOption(element, "apiVersion"));
}
return element;
}
@Override
public void loadState(Element state) {
super.loadState(state);
@@ -51,6 +78,6 @@ public class KotlinCommonCompilerArgumentsHolder extends BaseKotlinCompilerSetti
@NotNull
@Override
protected CommonCompilerArguments createSettings() {
return new CommonCompilerArguments.DummyImpl();
return CommonCompilerArguments.createDefaultInstance();
}
}
@@ -23,7 +23,7 @@ import org.jdom.Element
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
private fun Element.getOption(name: String) = getChildren("option").firstOrNull { it.getAttribute("name").value == name }
fun Element.getOption(name: String) = getChildren("option").firstOrNull { it.getAttribute("name").value == name }
private fun Element.getOptionValue(name: String) = getOption(name)?.getAttribute("value")?.value
@@ -33,13 +33,13 @@ import static org.jetbrains.kotlin.config.SettingConstants.KOTLIN_TO_JS_COMPILER
)
public class Kotlin2JsCompilerArgumentsHolder extends BaseKotlinCompilerSettings<K2JSCompilerArguments> {
@NotNull
@Override
protected K2JSCompilerArguments createSettings() {
return new K2JSCompilerArguments();
}
public static Kotlin2JsCompilerArgumentsHolder getInstance(Project project) {
return ServiceManager.getService(project, Kotlin2JsCompilerArgumentsHolder.class);
}
@NotNull
@Override
protected K2JSCompilerArguments createSettings() {
return K2JSCompilerArguments.createDefaultInstance();
}
}
@@ -21,8 +21,8 @@ import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
import static org.jetbrains.kotlin.idea.compiler.configuration.BaseKotlinCompilerSettings.KOTLIN_COMPILER_SETTINGS_PATH;
import static org.jetbrains.kotlin.config.SettingConstants.KOTLIN_TO_JVM_COMPILER_ARGUMENTS_SECTION;
import static org.jetbrains.kotlin.idea.compiler.configuration.BaseKotlinCompilerSettings.KOTLIN_COMPILER_SETTINGS_PATH;
@State(
name = KOTLIN_TO_JVM_COMPILER_ARGUMENTS_SECTION,
@@ -33,13 +33,13 @@ import static org.jetbrains.kotlin.config.SettingConstants.KOTLIN_TO_JVM_COMPILE
)
public class Kotlin2JvmCompilerArgumentsHolder extends BaseKotlinCompilerSettings<K2JVMCompilerArguments> {
@NotNull
@Override
protected K2JVMCompilerArguments createSettings() {
return new K2JVMCompilerArguments();
}
public static Kotlin2JvmCompilerArgumentsHolder getInstance(Project project) {
return ServiceManager.getService(project, Kotlin2JvmCompilerArgumentsHolder.class);
}
@NotNull
@Override
protected K2JVMCompilerArguments createSettings() {
return K2JVMCompilerArguments.createDefaultInstance();
}
}
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>
@@ -0,0 +1,3 @@
<component name="CopyrightManager">
<settings default="" />
</component>
@@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/kotlin-runtime.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
</SOURCES>
</library>
</component>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/module.iml" filepath="$PROJECT_DIR$/module.iml" />
</modules>
</component>
</project>
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>
@@ -0,0 +1,3 @@
<component name="CopyrightManager">
<settings default="" />
</component>
@@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/kotlin-runtime.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
</SOURCES>
</library>
</component>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/module.iml" filepath="$PROJECT_DIR$/module.iml" />
</modules>
</component>
</project>
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>
@@ -0,0 +1,238 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* 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 org.jetbrains.kotlin.idea.configuration;
import com.intellij.openapi.application.PathMacros;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.testFramework.PlatformTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.utils.PathUtil;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.kotlin.idea.configuration.KotlinWithLibraryConfigurator.FileState;
import static org.jetbrains.kotlin.idea.configuration.KotlinWithLibraryConfigurator.LibraryState;
public abstract class AbstractConfigureKotlinTest extends PlatformTestCase {
private static final String BASE_PATH = "idea/testData/configuration/";
private static final String TEMP_DIR_MACRO_KEY = "TEMP_TEST_DIR";
protected static final KotlinJavaModuleConfigurator JAVA_CONFIGURATOR = new KotlinJavaModuleConfigurator() {
@Override
protected String getDefaultPathToJarFile(@NotNull Project project) {
return getPathRelativeToTemp("default_jvm_lib");
}
};
protected static final KotlinJsModuleConfigurator JS_CONFIGURATOR = new KotlinJsModuleConfigurator() {
@Override
protected String getDefaultPathToJarFile(@NotNull Project project) {
return getPathRelativeToTemp("default_js_lib");
}
};
private static void configure(
@NotNull List<Module> modules,
@NotNull FileState runtimeState,
@NotNull LibraryState libraryState,
@NotNull KotlinWithLibraryConfigurator configurator,
@NotNull String jarFromDist,
@NotNull String jarFromTemp
) {
Project project = modules.iterator().next().getProject();
NotificationMessageCollector collector = NotificationMessageCollectorKt.createConfigureKotlinNotificationCollector(project);
for (Module module : modules) {
String pathToJar = getPathToJar(runtimeState, jarFromDist, jarFromTemp);
configurator.configureModuleWithLibraryClasses(module, libraryState, runtimeState, pathToJar, collector);
}
collector.showNotification();
}
@NotNull
private static String getPathToJar(@NotNull FileState runtimeState, @NotNull String jarFromDist, @NotNull String jarFromTemp) {
switch (runtimeState) {
case EXISTS:
return jarFromDist;
case COPY:
return jarFromTemp;
case DO_NOT_COPY:
return jarFromDist;
}
return jarFromDist;
}
protected static void configure(@NotNull Module module, @NotNull FileState jarState, @NotNull LibraryState libraryState, @NotNull KotlinProjectConfigurator configurator) {
if (configurator instanceof KotlinJavaModuleConfigurator) {
configure(Collections.singletonList(module), jarState, libraryState,
(KotlinWithLibraryConfigurator) configurator,
getPathToExistentRuntimeJar(), getPathToNonexistentRuntimeJar());
}
if (configurator instanceof KotlinJsModuleConfigurator) {
configure(Collections.singletonList(module), jarState, libraryState,
(KotlinWithLibraryConfigurator) configurator,
getPathToExistentJsJar(), getPathToNonexistentJsJar());
}
}
private static String getPathToNonexistentRuntimeJar() {
String pathToTempKotlinRuntimeJar = FileUtil.getTempDirectory() + "/" + PathUtil.KOTLIN_JAVA_RUNTIME_JAR;
myFilesToDelete.add(new File(pathToTempKotlinRuntimeJar));
return pathToTempKotlinRuntimeJar;
}
private static String getPathToNonexistentJsJar() {
String pathToTempKotlinRuntimeJar = FileUtil.getTempDirectory() + "/" + PathUtil.JS_LIB_JAR_NAME;
myFilesToDelete.add(new File(pathToTempKotlinRuntimeJar));
return pathToTempKotlinRuntimeJar;
}
private static String getPathToExistentRuntimeJar() {
return PathUtil.getKotlinPathsForDistDirectory().getRuntimePath().getParent();
}
private static String getPathToExistentJsJar() {
return PathUtil.getKotlinPathsForDistDirectory().getJsStdLibJarPath().getParent();
}
protected static void assertNotConfigured(Module module, KotlinWithLibraryConfigurator configurator) {
assertFalse(
String.format("Module %s should not be configured as %s Module", module.getName(), configurator.getPresentableText()),
configurator.isConfigured(module));
}
protected static void assertConfigured(Module module, KotlinWithLibraryConfigurator configurator) {
assertTrue(String.format("Module %s should be configured with configurator '%s'", module.getName(),
configurator.getPresentableText()),
configurator.isConfigured(module));
}
protected static void assertProperlyConfigured(Module module, KotlinWithLibraryConfigurator configurator) {
assertConfigured(module, configurator);
assertNotConfigured(module, getOppositeConfigurator(configurator));
}
private static KotlinWithLibraryConfigurator getOppositeConfigurator(KotlinWithLibraryConfigurator configurator) {
if (configurator == JAVA_CONFIGURATOR) return JS_CONFIGURATOR;
if (configurator == JS_CONFIGURATOR) return JAVA_CONFIGURATOR;
throw new IllegalArgumentException("Only JS_CONFIGURATOR and JAVA_CONFIGURATOR are supported");
}
private static String getPathRelativeToTemp(String relativePath) {
String tempPath = PathMacros.getInstance().getValue(TEMP_DIR_MACRO_KEY);
return tempPath + '/' + relativePath;
}
@Override
protected void tearDown() throws Exception {
PathMacros.getInstance().removeMacro(TEMP_DIR_MACRO_KEY);
super.tearDown();
}
@Override
protected void initApplication() throws Exception {
super.initApplication();
File tempLibDir = FileUtil.createTempDirectory("temp", null);
PathMacros.getInstance().setMacro(TEMP_DIR_MACRO_KEY, FileUtilRt.toSystemDependentName(tempLibDir.getAbsolutePath()));
}
protected void doTestConfigureModulesWithNonDefaultSetup(KotlinWithLibraryConfigurator configurator) {
assertNoFilesInDefaultPaths();
Module[] modules = getModules();
for (Module module : modules) {
assertNotConfigured(module, configurator);
}
configurator.configure(myProject, Collections.<Module>emptyList());
assertNoFilesInDefaultPaths();
for (Module module : modules) {
assertProperlyConfigured(module, configurator);
}
}
protected void doTestOneJavaModule(@NotNull FileState jarState, @NotNull LibraryState libraryState) {
doTestOneModule(jarState, libraryState, JAVA_CONFIGURATOR);
}
protected void doTestOneJsModule(@NotNull FileState jarState, @NotNull LibraryState libraryState) {
doTestOneModule(jarState, libraryState, JS_CONFIGURATOR);
}
private void doTestOneModule(@NotNull FileState jarState, @NotNull LibraryState libraryState, @NotNull KotlinWithLibraryConfigurator configurator) {
Module module = getModule();
assertEquals("Library state loaded from project files should be " + libraryState, libraryState, configurator.getLibraryState(module.getProject()));
assertNotConfigured(module, configurator);
configure(module, jarState, libraryState, configurator);
assertProperlyConfigured(module, configurator);
}
@Override
public Module getModule() {
Module[] modules = ModuleManager.getInstance(myProject).getModules();
assert modules.length == 1 : "One module should be loaded " + modules.length;
myModule = modules[0];
return super.getModule();
}
public Module[] getModules() {
return ModuleManager.getInstance(myProject).getModules();
}
@Override
protected File getIprFile() throws IOException {
String projectFilePath = getProjectRoot() + "/projectFile.ipr";
assertTrue("Project file should exists " + projectFilePath, new File(projectFilePath).exists());
return new File(projectFilePath);
}
@Override
protected Project doCreateProject(@NotNull File projectFile) throws Exception {
return myProjectManager.loadProject(projectFile.getPath());
}
private String getProjectName() {
String testName = getTestName(true);
if (testName.contains("_")) {
return testName.substring(0, testName.indexOf("_"));
}
return testName;
}
protected String getProjectRoot() {
return BASE_PATH + getProjectName();
}
@Override
protected void setUpModule() {
}
private void assertNoFilesInDefaultPaths() {
assertDoesntExist(new File(JAVA_CONFIGURATOR.getDefaultPathToJarFile(getProject())));
assertDoesntExist(new File(JS_CONFIGURATOR.getDefaultPathToJarFile(getProject())));
}
}
@@ -0,0 +1,65 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* 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 org.jetbrains.kotlin.idea.configuration
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.impl.ApplicationImpl
import com.intellij.openapi.util.io.FileUtil
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.idea.project.getLanguageVersionSettings
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.junit.Assert
import java.io.File
import java.io.IOException
class ConfigureKotlinInTempDirTest : AbstractConfigureKotlinTest() {
@Throws(IOException::class)
override fun getIprFile(): File {
val tempDir = FileUtil.generateRandomTemporaryPath()
FileUtil.createTempDirectory("temp", null)
FileUtil.copyDir(File(projectRoot), tempDir)
val projectRoot = tempDir.path
val projectFilePath = projectRoot + "/projectFile.ipr"
if (!File(projectFilePath).exists()) {
val dotIdeaPath = projectRoot + "/.idea"
Assert.assertTrue("Project file or '.idea' dir should exists in " + projectRoot, File(dotIdeaPath).exists())
return File(projectRoot)
}
return File(projectFilePath)
}
@Throws(IOException::class)
fun testKotlincExistsNoSettingsRuntime10() {
val application = ApplicationManager.getApplication() as ApplicationImpl
application.doNotSave(false)
Assert.assertEquals(LanguageVersion.KOTLIN_1_0, myProject.getLanguageVersionSettings(null).languageVersion)
Assert.assertEquals(LanguageVersion.KOTLIN_1_0, module.languageVersionSettings.languageVersion)
application.saveAll()
Assert.assertTrue(project.baseDir.findFileByRelativePath(".idea/kotlinc.xml") != null)
}
fun testKotlincExistsNoSettingsRuntime11() {
val application = ApplicationManager.getApplication() as ApplicationImpl
application.doNotSave(false)
Assert.assertEquals(LanguageVersion.KOTLIN_1_1, myProject.getLanguageVersionSettings(null).languageVersion)
Assert.assertEquals(LanguageVersion.KOTLIN_1_1, module.languageVersionSettings.languageVersion)
Assert.assertTrue(project.baseDir.findFileByRelativePath(".idea/kotlinc.xml") == null)
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,162 +16,29 @@
package org.jetbrains.kotlin.idea.configuration;
import com.intellij.openapi.application.PathMacros;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.testFramework.PlatformTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments;
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
import org.jetbrains.kotlin.config.*;
import org.jetbrains.kotlin.idea.project.PlatformKt;
import org.jetbrains.kotlin.utils.PathUtil;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.kotlin.idea.configuration.KotlinWithLibraryConfigurator.FileState;
import static org.jetbrains.kotlin.idea.configuration.KotlinWithLibraryConfigurator.LibraryState;
public class ConfigureKotlinTest extends PlatformTestCase {
private static final String BASE_PATH = "idea/testData/configuration/";
private static final String TEMP_DIR_MACRO_KEY = "TEMP_TEST_DIR";
private static final KotlinJavaModuleConfigurator JAVA_CONFIGURATOR = new KotlinJavaModuleConfigurator() {
@Override
protected String getDefaultPathToJarFile(@NotNull Project project) {
return getPathRelativeToTemp("default_jvm_lib");
}
};
private static final KotlinJsModuleConfigurator JS_CONFIGURATOR = new KotlinJsModuleConfigurator() {
@Override
protected String getDefaultPathToJarFile(@NotNull Project project) {
return getPathRelativeToTemp("default_js_lib");
}
};
private static void configure(
@NotNull List<Module> modules,
@NotNull FileState runtimeState,
@NotNull LibraryState libraryState,
@NotNull KotlinWithLibraryConfigurator configurator,
@NotNull String jarFromDist,
@NotNull String jarFromTemp
) {
Project project = modules.iterator().next().getProject();
NotificationMessageCollector collector = NotificationMessageCollectorKt.createConfigureKotlinNotificationCollector(project);
for (Module module : modules) {
String pathToJar = getPathToJar(runtimeState, jarFromDist, jarFromTemp);
configurator.configureModuleWithLibraryClasses(module, libraryState, runtimeState, pathToJar, collector);
}
collector.showNotification();
}
@NotNull
private static String getPathToJar(@NotNull FileState runtimeState, @NotNull String jarFromDist, @NotNull String jarFromTemp) {
switch (runtimeState) {
case EXISTS:
return jarFromDist;
case COPY:
return jarFromTemp;
case DO_NOT_COPY:
return jarFromDist;
}
return jarFromDist;
}
private static void configure(@NotNull Module module, @NotNull FileState jarState, @NotNull LibraryState libraryState, @NotNull KotlinProjectConfigurator configurator) {
if (configurator instanceof KotlinJavaModuleConfigurator) {
configure(Collections.singletonList(module), jarState, libraryState,
(KotlinWithLibraryConfigurator) configurator,
getPathToExistentRuntimeJar(), getPathToNonexistentRuntimeJar());
}
if (configurator instanceof KotlinJsModuleConfigurator) {
configure(Collections.singletonList(module), jarState, libraryState,
(KotlinWithLibraryConfigurator) configurator,
getPathToExistentJsJar(), getPathToNonexistentJsJar());
}
}
private static String getPathToNonexistentRuntimeJar() {
String pathToTempKotlinRuntimeJar = FileUtil.getTempDirectory() + "/" + PathUtil.KOTLIN_JAVA_RUNTIME_JAR;
myFilesToDelete.add(new File(pathToTempKotlinRuntimeJar));
return pathToTempKotlinRuntimeJar;
}
private static String getPathToNonexistentJsJar() {
String pathToTempKotlinRuntimeJar = FileUtil.getTempDirectory() + "/" + PathUtil.JS_LIB_JAR_NAME;
myFilesToDelete.add(new File(pathToTempKotlinRuntimeJar));
return pathToTempKotlinRuntimeJar;
}
private static String getPathToExistentRuntimeJar() {
return PathUtil.getKotlinPathsForDistDirectory().getRuntimePath().getParent();
}
private static String getPathToExistentJsJar() {
return PathUtil.getKotlinPathsForDistDirectory().getJsStdLibJarPath().getParent();
}
private static void assertNotConfigured(Module module, KotlinWithLibraryConfigurator configurator) {
assertFalse(
String.format("Module %s should not be configured as %s Module", module.getName(), configurator.getPresentableText()),
configurator.isConfigured(module));
}
private static void assertConfigured(Module module, KotlinWithLibraryConfigurator configurator) {
assertTrue(String.format("Module %s should be configured with configurator '%s'", module.getName(),
configurator.getPresentableText()),
configurator.isConfigured(module));
}
private static void assertProperlyConfigured(Module module, KotlinWithLibraryConfigurator configurator) {
assertConfigured(module, configurator);
assertNotConfigured(module, getOppositeConfigurator(configurator));
}
private static KotlinWithLibraryConfigurator getOppositeConfigurator(KotlinWithLibraryConfigurator configurator) {
if (configurator == JAVA_CONFIGURATOR) return JS_CONFIGURATOR;
if (configurator == JS_CONFIGURATOR) return JAVA_CONFIGURATOR;
throw new IllegalArgumentException("Only JS_CONFIGURATOR and JAVA_CONFIGURATOR are supported");
}
private static String getPathRelativeToTemp(String relativePath) {
String tempPath = PathMacros.getInstance().getValue(TEMP_DIR_MACRO_KEY);
return tempPath + '/' + relativePath;
}
@Override
protected void tearDown() throws Exception {
PathMacros.getInstance().removeMacro(TEMP_DIR_MACRO_KEY);
super.tearDown();
}
@Override
protected void initApplication() throws Exception {
super.initApplication();
File tempLibDir = FileUtil.createTempDirectory("temp", null);
PathMacros.getInstance().setMacro(TEMP_DIR_MACRO_KEY, FileUtilRt.toSystemDependentName(tempLibDir.getAbsolutePath()));
}
public class ConfigureKotlinTest extends AbstractConfigureKotlinTest {
public void testNewLibrary_copyJar() {
doTestOneJavaModule(FileState.COPY, LibraryState.NEW_LIBRARY);
doTestOneJavaModule(KotlinWithLibraryConfigurator.FileState.COPY, KotlinWithLibraryConfigurator.LibraryState.NEW_LIBRARY);
}
public void testNewLibrary_doNotCopyJar() {
doTestOneJavaModule(FileState.DO_NOT_COPY, LibraryState.NEW_LIBRARY);
doTestOneJavaModule(KotlinWithLibraryConfigurator.FileState.DO_NOT_COPY, KotlinWithLibraryConfigurator.LibraryState.NEW_LIBRARY);
}
public void testLibrary_doNotCopyJar() {
try {
doTestOneJavaModule(FileState.DO_NOT_COPY, LibraryState.LIBRARY);
doTestOneJavaModule(KotlinWithLibraryConfigurator.FileState.DO_NOT_COPY, KotlinWithLibraryConfigurator.LibraryState.LIBRARY);
}
catch (IllegalStateException e) {
return;
@@ -180,19 +47,19 @@ public class ConfigureKotlinTest extends PlatformTestCase {
}
public void testLibraryWithoutPaths_jarExists() {
doTestOneJavaModule(FileState.EXISTS, LibraryState.NON_CONFIGURED_LIBRARY);
doTestOneJavaModule(KotlinWithLibraryConfigurator.FileState.EXISTS, KotlinWithLibraryConfigurator.LibraryState.NON_CONFIGURED_LIBRARY);
}
public void testNewLibrary_jarExists() {
doTestOneJavaModule(FileState.EXISTS, LibraryState.NEW_LIBRARY);
doTestOneJavaModule(KotlinWithLibraryConfigurator.FileState.EXISTS, KotlinWithLibraryConfigurator.LibraryState.NEW_LIBRARY);
}
public void testLibraryWithoutPaths_copyJar() {
doTestOneJavaModule(FileState.COPY, LibraryState.NON_CONFIGURED_LIBRARY);
doTestOneJavaModule(KotlinWithLibraryConfigurator.FileState.COPY, KotlinWithLibraryConfigurator.LibraryState.NON_CONFIGURED_LIBRARY);
}
public void testLibraryWithoutPaths_doNotCopyJar() {
doTestOneJavaModule(FileState.DO_NOT_COPY, LibraryState.NON_CONFIGURED_LIBRARY);
doTestOneJavaModule(KotlinWithLibraryConfigurator.FileState.DO_NOT_COPY, KotlinWithLibraryConfigurator.LibraryState.NON_CONFIGURED_LIBRARY);
}
@SuppressWarnings("ConstantConditions")
@@ -200,12 +67,12 @@ public class ConfigureKotlinTest extends PlatformTestCase {
Module[] modules = getModules();
for (Module module : modules) {
if (module.getName().equals("module1")) {
configure(module, KotlinWithLibraryConfigurator.FileState.DO_NOT_COPY, LibraryState.NEW_LIBRARY, JAVA_CONFIGURATOR);
configure(module, KotlinWithLibraryConfigurator.FileState.DO_NOT_COPY, KotlinWithLibraryConfigurator.LibraryState.NEW_LIBRARY, JAVA_CONFIGURATOR);
assertConfigured(module, JAVA_CONFIGURATOR);
}
else if (module.getName().equals("module2")) {
assertNotConfigured(module, JAVA_CONFIGURATOR);
configure(module, FileState.EXISTS, LibraryState.LIBRARY, JAVA_CONFIGURATOR);
configure(module, KotlinWithLibraryConfigurator.FileState.EXISTS, KotlinWithLibraryConfigurator.LibraryState.LIBRARY, JAVA_CONFIGURATOR);
assertConfigured(module, JAVA_CONFIGURATOR);
}
}
@@ -233,20 +100,20 @@ public class ConfigureKotlinTest extends PlatformTestCase {
}
public void testNewLibrary_jarExists_js() {
doTestOneJsModule(FileState.EXISTS, LibraryState.NEW_LIBRARY);
doTestOneJsModule(KotlinWithLibraryConfigurator.FileState.EXISTS, KotlinWithLibraryConfigurator.LibraryState.NEW_LIBRARY);
}
public void testNewLibrary_copyJar_js() {
doTestOneJsModule(FileState.COPY, LibraryState.NEW_LIBRARY);
doTestOneJsModule(KotlinWithLibraryConfigurator.FileState.COPY, KotlinWithLibraryConfigurator.LibraryState.NEW_LIBRARY);
}
public void testNewLibrary_doNotCopyJar_js() {
doTestOneJsModule(FileState.DO_NOT_COPY, LibraryState.NEW_LIBRARY);
doTestOneJsModule(KotlinWithLibraryConfigurator.FileState.DO_NOT_COPY, KotlinWithLibraryConfigurator.LibraryState.NEW_LIBRARY);
}
public void testJsLibrary_doNotCopyJar() {
try {
doTestOneJsModule(FileState.DO_NOT_COPY, LibraryState.LIBRARY);
doTestOneJsModule(KotlinWithLibraryConfigurator.FileState.DO_NOT_COPY, KotlinWithLibraryConfigurator.LibraryState.LIBRARY);
}
catch (IllegalStateException e) {
return;
@@ -255,15 +122,15 @@ public class ConfigureKotlinTest extends PlatformTestCase {
}
public void testJsLibraryWithoutPaths_jarExists() {
doTestOneJsModule(FileState.EXISTS, LibraryState.NON_CONFIGURED_LIBRARY);
doTestOneJsModule(KotlinWithLibraryConfigurator.FileState.EXISTS, KotlinWithLibraryConfigurator.LibraryState.NON_CONFIGURED_LIBRARY);
}
public void testJsLibraryWithoutPaths_copyJar() {
doTestOneJsModule(FileState.COPY, LibraryState.NON_CONFIGURED_LIBRARY);
doTestOneJsModule(KotlinWithLibraryConfigurator.FileState.COPY, KotlinWithLibraryConfigurator.LibraryState.NON_CONFIGURED_LIBRARY);
}
public void testJsLibraryWithoutPaths_doNotCopyJar() {
doTestOneJsModule(FileState.DO_NOT_COPY, LibraryState.NON_CONFIGURED_LIBRARY);
doTestOneJsModule(KotlinWithLibraryConfigurator.FileState.DO_NOT_COPY, KotlinWithLibraryConfigurator.LibraryState.NON_CONFIGURED_LIBRARY);
}
public void testProjectWithoutFacetWithRuntime106WithoutLanguageLevel() {
@@ -345,80 +212,4 @@ public class ConfigureKotlinTest extends PlatformTestCase {
assertEquals("amd", arguments.moduleKind);
assertEquals("-version -meta-info", settings.getCompilerSettings().additionalArguments);
}
private void doTestConfigureModulesWithNonDefaultSetup(KotlinWithLibraryConfigurator configurator) {
assertNoFilesInDefaultPaths();
Module[] modules = getModules();
for (Module module : modules) {
assertNotConfigured(module, configurator);
}
configurator.configure(myProject, Collections.<Module>emptyList());
assertNoFilesInDefaultPaths();
for (Module module : modules) {
assertProperlyConfigured(module, configurator);
}
}
private void doTestOneJavaModule(@NotNull FileState jarState, @NotNull LibraryState libraryState) {
doTestOneModule(jarState, libraryState, JAVA_CONFIGURATOR);
}
private void doTestOneJsModule(@NotNull FileState jarState, @NotNull LibraryState libraryState) {
doTestOneModule(jarState, libraryState, JS_CONFIGURATOR);
}
private void doTestOneModule(@NotNull FileState jarState, @NotNull LibraryState libraryState, @NotNull KotlinWithLibraryConfigurator configurator) {
Module module = getModule();
assertEquals("Library state loaded from project files should be " + libraryState, libraryState, configurator.getLibraryState(module.getProject()));
assertNotConfigured(module, configurator);
configure(module, jarState, libraryState, configurator);
assertProperlyConfigured(module, configurator);
}
@Override
public Module getModule() {
Module[] modules = ModuleManager.getInstance(myProject).getModules();
assert modules.length == 1 : "One module should be loaded " + modules.length;
myModule = modules[0];
return super.getModule();
}
public Module[] getModules() {
return ModuleManager.getInstance(myProject).getModules();
}
@Override
protected File getIprFile() throws IOException {
String projectName = getProjectName();
String projectFilePath = BASE_PATH + projectName + "/projectFile.ipr";
assertTrue("Project file should exists " + projectFilePath, new File(projectFilePath).exists());
return new File(projectFilePath);
}
@Override
protected Project doCreateProject(@NotNull File projectFile) throws Exception {
return myProjectManager.loadProject(projectFile.getPath());
}
private String getProjectName() {
String testName = getTestName(true);
if (testName.contains("_")) {
return testName.substring(0, testName.indexOf("_"));
}
return testName;
}
@Override
protected void setUpModule() {
}
private void assertNoFilesInDefaultPaths() {
assertDoesntExist(new File(JAVA_CONFIGURATOR.getDefaultPathToJarFile(getProject())));
assertDoesntExist(new File(JS_CONFIGURATOR.getDefaultPathToJarFile(getProject())));
}
}