Skip to content

Commit

Permalink
Polish GradleExtension tests (#1908)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Nov 27, 2023
2 parents 2b05af1 + f183b61 commit c4f8a88
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,24 +17,20 @@

import java.io.IOException;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import com.diffplug.common.base.StringPrinter;

class GroovyGradleExtensionTest extends GradleIntegrationHarness {
/**
* We test GroovyGradleExtension only behaviors here.
*/
class GroovyGradleExtensionTest extends GroovyExtensionTest {
private static final String HEADER = "//My tests header";

@Test
void defaultTarget() throws IOException {
testTarget(true);
}

@Test
void customTarget() throws IOException {
testTarget(false);
}

private void testTarget(boolean useDefaultTarget) throws IOException {
@ParameterizedTest
@ValueSource(booleans = {true, false})
void testTarget(boolean useDefaultTarget) throws IOException {
String target = useDefaultTarget ? "" : "target 'other.gradle'";
String buildContent = StringPrinter.buildStringFromLines(
"plugins {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class KotlinExtensionTest extends GradleIntegrationHarness {
void integrationDiktat() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.4.30'",
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -48,7 +48,7 @@ void integrationDiktat() throws IOException {
void integrationKtfmt_dropboxStyle_0_19() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -66,7 +66,7 @@ void integrationKtfmt_dropboxStyle_0_19() throws IOException {
void withExperimentalEditorConfigOverride() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -90,7 +90,7 @@ void testWithInvalidEditorConfigFile() throws IOException {

setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -110,7 +110,7 @@ void testReadCodeStyleFromEditorConfigFile() throws IOException {
setFile(".editorconfig").toResource("kotlin/ktlint/ktlint_official/.editorconfig");
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -127,7 +127,7 @@ void testSetEditorConfigCanOverrideEditorConfigFile() throws IOException {
setFile(".editorconfig").toResource("kotlin/ktlint/intellij_idea/.editorconfig");
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -145,7 +145,7 @@ void testSetEditorConfigCanOverrideEditorConfigFile() throws IOException {
void withCustomRuleSetApply() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -169,7 +169,7 @@ void withCustomRuleSetApply() throws IOException {
void testWithHeader() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand All @@ -188,7 +188,7 @@ void testWithHeader() throws IOException {
void testWithCustomMaxWidthDefaultStyleKtfmt() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,28 @@
*/
package com.diffplug.gradle.spotless;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;

import org.gradle.testkit.runner.BuildResult;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class KotlinGradleExtensionTest extends GradleIntegrationHarness {
@Test
void integration_default_diktat() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.4.30'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlinGradle {",
" diktat()",
" }",
"}");
setFile("configuration.gradle.kts").toResource("kotlin/diktat/basic.dirty");
BuildResult result = gradleRunner().withArguments("spotlessApply").buildAndFail();
assertThat(result.getOutput()).contains("[AVOID_NESTED_FUNCTIONS] try to avoid using nested functions");
}
/**
* We test KotlinGradleExtension only behaviors here.
*/
class KotlinGradleExtensionTest extends KotlinExtensionTest {

@Test
void withExperimentalEditorConfigOverride() throws IOException {
@ParameterizedTest
@ValueSource(booleans = {true, false})
void testTarget(boolean useDefaultTarget) throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'org.jetbrains.kotlin.jvm' version '1.6.21'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlinGradle {",
" " + (useDefaultTarget ? "" : "target \"*.kts\""),
" ktlint().editorConfigOverride([",
" ktlint_experimental: \"enabled\",",
" ij_kotlin_allow_trailing_comma: true,",
Expand All @@ -59,25 +45,14 @@ void withExperimentalEditorConfigOverride() throws IOException {
" }",
"}");
setFile("configuration.gradle.kts").toResource("kotlin/ktlint/experimentalEditorConfigOverride.dirty");
setFile("configuration.kts").toResource("kotlin/ktlint/experimentalEditorConfigOverride.dirty");
gradleRunner().withArguments("spotlessApply").build();
assertFile("configuration.gradle.kts").sameAsResource("kotlin/ktlint/experimentalEditorConfigOverride.clean");
}

@Test
void integration_ktfmt_with_dropbox_style() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'org.jetbrains.kotlin.jvm' version '1.5.31'",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlinGradle {",
" ktfmt().dropboxStyle()",
" }",
"}");
setFile("configuration.gradle.kts").toResource("kotlin/ktfmt/dropboxstyle.dirty");
gradleRunner().withArguments("spotlessApply").build();
assertFile("configuration.gradle.kts").sameAsResource("kotlin/ktfmt/dropboxstyle.clean");
if (useDefaultTarget) {
assertFile("configuration.gradle.kts").sameAsResource("kotlin/ktlint/experimentalEditorConfigOverride.clean");
assertFile("configuration.kts").sameAsResource("kotlin/ktlint/experimentalEditorConfigOverride.dirty");
} else {
assertFile("configuration.gradle.kts").sameAsResource("kotlin/ktlint/experimentalEditorConfigOverride.clean");
assertFile("configuration.kts").sameAsResource("kotlin/ktlint/experimentalEditorConfigOverride.clean");
}
}
}

0 comments on commit c4f8a88

Please sign in to comment.