Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a placeholder for years #1640

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
<mainClass>org.cqfn.diktat.ruleset.generation.GenerationKt</mainClass>
<arguments>
<argument>${project.build.sourceDirectory}</argument>
<argument>${project.basedir}/src/test/resources</argument>
</arguments>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,14 @@
package org.cqfn.diktat.ruleset.generation

import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.rules.chapter2.comments.HeaderCommentRule.Companion.afterCopyrightRegex
import org.cqfn.diktat.ruleset.rules.chapter2.comments.HeaderCommentRule.Companion.curYear
import org.cqfn.diktat.ruleset.rules.chapter2.comments.HeaderCommentRule.Companion.hyphenRegex

import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec

import java.nio.file.Files
import java.nio.file.Paths

import kotlin.io.path.createTempFile
import kotlin.io.path.name
import kotlin.io.path.readLines
import kotlin.io.path.writeLines

/**
* The comment that will be added to the generated sources file.
*/
Expand All @@ -32,11 +23,10 @@ private val autoGenerationComment =
""".trimMargin()

fun main(args: Array<String>) {
require(args.size == 2) {
"Only two arguments are expected: <source root> <test resource root>"
require(args.size == 1) {
"Expected only one argument: <source root>"
}
generateWarningNames(args[0])
validateYear(args[1])
}

private fun generateWarningNames(sourceDirectory: String) {
Expand Down Expand Up @@ -64,28 +54,3 @@ private fun generateWarningNames(sourceDirectory: String) {

kotlinFile.writeTo(Paths.get(sourceDirectory))
}

private fun validateYear(testResourcesDirectory: String) {
val folder = Paths.get(testResourcesDirectory, "test/paragraph2/header")
Files.list(folder)
.filter { !it.name.contains("CopyrightDifferentYearTest.kt") }
.forEach { file ->
val tempFile = createTempFile()
tempFile.writeLines(file.readLines()
.map { line ->
when {
line.contains(hyphenRegex) -> line.replace(hyphenRegex) {
val years = it.value.split("-")
"${years[0]}-$curYear"
}
line.contains(afterCopyrightRegex) -> line.replace(afterCopyrightRegex) {
val copyrightYears = it.value.split("(c)", "(C)", "©")
"${copyrightYears[0]}-$curYear"
}
else -> line
}
})
Files.delete(file)
Files.move(tempFile, file)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,39 @@ class HeaderCommentRuleFixTest : FixTestBase(
RulesConfig("HEADER_MISSING_OR_WRONG_COPYRIGHT", true,
mapOf(
"isCopyrightMandatory" to "true",
"copyrightText" to "Copyright (c) Huawei Technologies Co., Ltd. 2020-${LocalDate.now().year}. All rights reserved.")
"copyrightText" to "Copyright (c) Huawei Technologies Co., Ltd. 2020-$currentYear. All rights reserved.")
),
RulesConfig("HEADER_WRONG_FORMAT", true, emptyMap())
)
) {
@Test
@Tag(WarningNames.HEADER_WRONG_FORMAT)
fun `new line should be inserted after header KDoc`() {
fixAndCompare("NewlineAfterHeaderKdocExpected.kt", "NewlineAfterHeaderKdocTest.kt")
fixAndCompare("NewlineAfterHeaderKdocExpected.kt", "NewlineAfterHeaderKdocTest.kt", replacements = currentYearReplacement)
}

@Test
@Tag(WarningNames.HEADER_MISSING_OR_WRONG_COPYRIGHT)
fun `if no copyright is present and mandatoryCopyright=true, it is added`() {
fixAndCompare("AutoCopyrightExpected.kt", "AutoCopyrightTest.kt")
fixAndCompare("AutoCopyrightExpected.kt", "AutoCopyrightTest.kt", replacements = currentYearReplacement)
}

@Test
@Tag(WarningNames.HEADER_MISSING_OR_WRONG_COPYRIGHT)
fun `if no copyright is present, added it and apply pattern for current year`() {
fixAndCompare("AutoCopyrightApplyPatternExpected.kt", "AutoCopyrightApplyPatternTest.kt",
listOf(RulesConfig(HEADER_MISSING_OR_WRONG_COPYRIGHT.name, true,
mapOf(
"isCopyrightMandatory" to "true",
"copyrightText" to "Copyright (c) Huawei Technologies Co., Ltd. 2020-;@currYear;. All rights reserved.")
listOf(
RulesConfig(
HEADER_MISSING_OR_WRONG_COPYRIGHT.name, true,
mapOf(
"isCopyrightMandatory" to "true",
"copyrightText" to "Copyright (c) Huawei Technologies Co., Ltd. 2020-;@currYear;. All rights reserved."
)
),
RulesConfig(HEADER_WRONG_FORMAT.name, true, emptyMap())
),
RulesConfig(HEADER_WRONG_FORMAT.name, true, emptyMap())))
replacements = currentYearReplacement,
)
}

/**
Expand All @@ -55,21 +61,22 @@ class HeaderCommentRuleFixTest : FixTestBase(
@Test
@Tag(WarningNames.HEADER_NOT_BEFORE_PACKAGE)
fun `header KDoc should be moved before package`() {
fixAndCompare("MisplacedHeaderKdocExpected.kt", "MisplacedHeaderKdocTest.kt")
fixAndCompare("MisplacedHeaderKdocExpected.kt", "MisplacedHeaderKdocTest.kt", replacements = currentYearReplacement)
}

@Test
@Tags(Tag(WarningNames.HEADER_MISSING_OR_WRONG_COPYRIGHT), Tag(WarningNames.HEADER_WRONG_FORMAT))
fun `header KDoc should be moved before package - no copyright`() {
fixAndCompare("MisplacedHeaderKdocNoCopyrightExpected.kt", "MisplacedHeaderKdocNoCopyrightTest.kt",
listOf(RulesConfig(HEADER_MISSING_OR_WRONG_COPYRIGHT.name, false, emptyMap()), RulesConfig(HEADER_WRONG_FORMAT.name, true, emptyMap()))
listOf(RulesConfig(HEADER_MISSING_OR_WRONG_COPYRIGHT.name, false, emptyMap()), RulesConfig(HEADER_WRONG_FORMAT.name, true, emptyMap())),
replacements = currentYearReplacement,
)
}

@Test
@Tags(Tag(WarningNames.HEADER_NOT_BEFORE_PACKAGE), Tag(WarningNames.HEADER_MISSING_OR_WRONG_COPYRIGHT))
fun `header KDoc should be moved before package - appended copyright`() {
fixAndCompare("MisplacedHeaderKdocAppendedCopyrightExpected.kt", "MisplacedHeaderKdocAppendedCopyrightTest.kt")
fixAndCompare("MisplacedHeaderKdocAppendedCopyrightExpected.kt", "MisplacedHeaderKdocAppendedCopyrightTest.kt", replacements = currentYearReplacement)
}

@Test
Expand All @@ -79,7 +86,8 @@ class HeaderCommentRuleFixTest : FixTestBase(
listOf(RulesConfig(HEADER_MISSING_OR_WRONG_COPYRIGHT.name, true, mapOf(
"isCopyrightMandatory" to "true",
"copyrightText" to "Copyright (c) My Company., Ltd. 2012-2019. All rights reserved."
)))
))),
replacements = currentYearReplacement,
)
}

Expand All @@ -90,7 +98,8 @@ class HeaderCommentRuleFixTest : FixTestBase(
listOf(RulesConfig(HEADER_MISSING_OR_WRONG_COPYRIGHT.name, true, mapOf(
"isCopyrightMandatory" to "true",
"copyrightText" to "Copyright (c) My Company., Ltd. 2021. All rights reserved."
)))
))),
replacements = currentYearReplacement,
)
}

Expand All @@ -101,7 +110,8 @@ class HeaderCommentRuleFixTest : FixTestBase(
listOf(RulesConfig(HEADER_MISSING_OR_WRONG_COPYRIGHT.name, true, mapOf(
"isCopyrightMandatory" to "true",
"copyrightText" to "Copyright (c) My Company., Ltd. 2012-2019. All rights reserved."
)))
))),
replacements = currentYearReplacement,
)
}

Expand All @@ -112,7 +122,8 @@ class HeaderCommentRuleFixTest : FixTestBase(
listOf(RulesConfig(HEADER_MISSING_OR_WRONG_COPYRIGHT.name, true, mapOf(
"isCopyrightMandatory" to "true",
"copyrightText" to "Copyright (c) My Company., Ltd. 2012-2019. All rights reserved."
)))
))),
replacements = currentYearReplacement,
)
}

Expand All @@ -123,7 +134,8 @@ class HeaderCommentRuleFixTest : FixTestBase(
listOf(RulesConfig(HEADER_MISSING_OR_WRONG_COPYRIGHT.name, true, mapOf(
"isCopyrightMandatory" to "true",
"copyrightText" to "Copyright (c) My Company., Ltd. 2012-2021. All rights reserved."
)))
))),
replacements = currentYearReplacement,
)
}

Expand All @@ -134,7 +146,7 @@ class HeaderCommentRuleFixTest : FixTestBase(
listOf(RulesConfig(HEADER_MISSING_OR_WRONG_COPYRIGHT.name, true, mapOf(
"isCopyrightMandatory" to "true",
"copyrightText" to """
| Copyright 2018-${LocalDate.now().year} John Doe.
| Copyright 2018-$currentYear John Doe.
|
| Licensed under the Apache License, Version 2.0 (the "License");
| you may not use this file except in compliance with the License.
Expand All @@ -148,7 +160,8 @@ class HeaderCommentRuleFixTest : FixTestBase(
| See the License for the specific language governing permissions and
| limitations under the License.
""".trimMargin()
)))
))),
replacements = currentYearReplacement,
)
}

Expand All @@ -165,7 +178,14 @@ class HeaderCommentRuleFixTest : FixTestBase(
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
""".trimMargin()
)))
))),
replacements = currentYearReplacement,
)
}

companion object {
private const val PLACEHOLDER = "%%YEAR%%"
private val currentYear = LocalDate.now().year.toString()
private val currentYearReplacement = mapOf(PLACEHOLDER to currentYear)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ open class FixTestBase(
* @param overrideRulesConfigList optional override to [defaultRulesConfigList]
* @param trimLastEmptyLine whether the last (empty) line should be
* discarded when reading the content of [testPath].
* @param replacements a map of replacements which will be applied to [expectedPath] and [testPath] before comparing.
* @see fixAndCompareContent
*/
protected fun fixAndCompare(
expectedPath: String,
testPath: String,
overrideRulesConfigList: List<RulesConfig>? = null,
trimLastEmptyLine: Boolean = false,
replacements: Map<String, String> = emptyMap(),
) {
val testComparatorUnit = testComparatorUnitSupplier(overrideRulesConfigList)
val result = testComparatorUnit
.compareFilesFromResources(expectedPath, testPath, trimLastEmptyLine)
.compareFilesFromResources(expectedPath, testPath, trimLastEmptyLine, replacements)
Assertions.assertTrue(
result.isSuccessful
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
Copyright (c) Huawei Technologies Co., Ltd. 2020-%%YEAR%%. All rights reserved.
*/

package test.paragraph2.header
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
Copyright (c) Huawei Technologies Co., Ltd. 2020-%%YEAR%%. All rights reserved.
*/

package test.paragraph2.header
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) My Company., Ltd. 2012-2023. All rights reserved.
Copyright (c) My Company., Ltd. 2012-%%YEAR%%. All rights reserved.
*/
/**
* Lorem ipsum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) My Company., Ltd. 2012-2023. All rights reserved.
Copyright (c) My Company., Ltd. 2012-%%YEAR%%. All rights reserved.
*/
/**
* Lorem ipsum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) My Company., Ltd. 2021-2023. All rights reserved.
Copyright (c) My Company., Ltd. 2021-%%YEAR%%. All rights reserved.
*/
/**
* Lorem ipsum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) My Company., Ltd. 2021-2023. All rights reserved.
Copyright (c) My Company., Ltd. 2021-%%YEAR%%. All rights reserved.
*/
/**
* Lorem ipsum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) My Company., Ltd. 2012-2023. All rights reserved.
Copyright (c) My Company., Ltd. 2012-%%YEAR%%. All rights reserved.
*/
/**
* Lorem ipsum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
Copyright (c) Huawei Technologies Co., Ltd. 2020-%%YEAR%%. All rights reserved.
*/
/**
* Lorem ipsum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
Copyright (c) Huawei Technologies Co., Ltd. 2020-%%YEAR%%. All rights reserved.
*/
/**
* Lorem ipsum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
Copyright (c) Huawei Technologies Co., Ltd. 2020-%%YEAR%%. All rights reserved.
*/

package test.paragraph2.header
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018-2023 John Doe.
Copyright 2018-%%YEAR%% John Doe.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018-2023 John Doe.
Copyright 2018-%%YEAR%% John Doe.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018-2023 John Doe.
Copyright 2018-%%YEAR%% John Doe.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
Copyright (c) Huawei Technologies Co., Ltd. 2020-%%YEAR%%. All rights reserved.
*/
/**
* This is a file used in unit test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
Copyright (c) Huawei Technologies Co., Ltd. 2020-%%YEAR%%. All rights reserved.
*/
/**
* This is a file used in unit test
Expand Down
Loading