diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java index dbb8f970d5c..7acdae33189 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java @@ -99,6 +99,15 @@ public abstract class CommonCompilerArguments implements Serializable { public List unknownExtraFlags = new SmartList(); + @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"; diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.java b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.java index cafc7fc278b..7162817cfba 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.java +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.java @@ -71,6 +71,13 @@ public class K2JSCompilerArguments extends CommonCompilerArguments { @ValueDescription("") public String outputPostfix; + @NotNull + public static K2JSCompilerArguments createDefaultInstance() { + K2JSCompilerArguments arguments = new K2JSCompilerArguments(); + arguments.moduleKind = K2JsArgumentConstants.MODULE_PLAIN; + return arguments; + } + @Override @NotNull public String executableScriptFileName() { diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java index 4d62eca82b4..f038db1ffa4 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java @@ -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"; } - } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.java index 7290df6f516..7235ebd3a75 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/BaseKotlinCompilerSettings.java @@ -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 implements PersistentStateComponent { 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; diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.java index 1591b923d3e..02fdbbf573f 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/compiler/configuration/KotlinCommonCompilerArgumentsHolder.java @@ -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 { + 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(); } } diff --git a/idea/idea-jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt b/idea/idea-jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt index c696d3bd62c..348190d62cb 100644 --- a/idea/idea-jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt +++ b/idea/idea-jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt @@ -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 diff --git a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JsCompilerArgumentsHolder.java b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JsCompilerArgumentsHolder.java index bedcefcdcbf..3d6d88fe760 100644 --- a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JsCompilerArgumentsHolder.java +++ b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JsCompilerArgumentsHolder.java @@ -33,13 +33,13 @@ import static org.jetbrains.kotlin.config.SettingConstants.KOTLIN_TO_JS_COMPILER ) public class Kotlin2JsCompilerArgumentsHolder extends BaseKotlinCompilerSettings { - @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(); + } } diff --git a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JvmCompilerArgumentsHolder.java b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JvmCompilerArgumentsHolder.java index 9c2b35a7fdf..b74f54fa99e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JvmCompilerArgumentsHolder.java +++ b/idea/src/org/jetbrains/kotlin/idea/compiler/configuration/Kotlin2JvmCompilerArgumentsHolder.java @@ -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 { - @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(); + } } diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/compiler.xml b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/compiler.xml new file mode 100644 index 00000000000..1c358218b27 --- /dev/null +++ b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/copyright/profiles_settings.xml b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/copyright/profiles_settings.xml new file mode 100644 index 00000000000..e7bedf3377d --- /dev/null +++ b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/libraries/KotlinJavaRuntime.xml b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 00000000000..bcc4ed94e22 --- /dev/null +++ b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/misc.xml b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/misc.xml new file mode 100644 index 00000000000..5a967a62c8a --- /dev/null +++ b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/modules.xml b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/modules.xml new file mode 100644 index 00000000000..83db3b6328a --- /dev/null +++ b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/lib/kotlin-runtime.jar b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/lib/kotlin-runtime.jar new file mode 100644 index 00000000000..c62396395dd Binary files /dev/null and b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/lib/kotlin-runtime.jar differ diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/module.iml b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/module.iml new file mode 100644 index 00000000000..94371e52a2a --- /dev/null +++ b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/module.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/src/foo.kt b/idea/testData/configuration/kotlincExistsNoSettingsRuntime10/src/foo.kt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/compiler.xml b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/compiler.xml new file mode 100644 index 00000000000..1c358218b27 --- /dev/null +++ b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/copyright/profiles_settings.xml b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/copyright/profiles_settings.xml new file mode 100644 index 00000000000..e7bedf3377d --- /dev/null +++ b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/libraries/KotlinJavaRuntime.xml b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 00000000000..bcc4ed94e22 --- /dev/null +++ b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/misc.xml b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/misc.xml new file mode 100644 index 00000000000..5a967a62c8a --- /dev/null +++ b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/modules.xml b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/modules.xml new file mode 100644 index 00000000000..83db3b6328a --- /dev/null +++ b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/lib/kotlin-runtime.jar b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/lib/kotlin-runtime.jar new file mode 100644 index 00000000000..24854eff673 Binary files /dev/null and b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/lib/kotlin-runtime.jar differ diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/module.iml b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/module.iml new file mode 100644 index 00000000000..94371e52a2a --- /dev/null +++ b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/module.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/src/foo.kt b/idea/testData/configuration/kotlincExistsNoSettingsRuntime11/src/foo.kt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/idea/tests/org/jetbrains/kotlin/idea/configuration/AbstractConfigureKotlinTest.java b/idea/tests/org/jetbrains/kotlin/idea/configuration/AbstractConfigureKotlinTest.java new file mode 100644 index 00000000000..e74cb42bc19 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/configuration/AbstractConfigureKotlinTest.java @@ -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 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.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()))); + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/configuration/ConfigureKotlinInTempDirTest.kt b/idea/tests/org/jetbrains/kotlin/idea/configuration/ConfigureKotlinInTempDirTest.kt new file mode 100644 index 00000000000..3204ac5813c --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/configuration/ConfigureKotlinInTempDirTest.kt @@ -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) + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/configuration/ConfigureKotlinTest.java b/idea/tests/org/jetbrains/kotlin/idea/configuration/ConfigureKotlinTest.java index 7223744409f..b6f237fd8d4 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/configuration/ConfigureKotlinTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/configuration/ConfigureKotlinTest.java @@ -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 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.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()))); - } }