Skip to content

Commit

Permalink
Improvements for Copyrights rule - support pattern for the current ye…
Browse files Browse the repository at this point in the history
…ar in the configuration (#1154)

### What's done:
* Improvements for Copyrights rule - support pattern for the current year in the configuration
  • Loading branch information
kgevorkyan authored Dec 21, 2021
1 parent 87ae5c0 commit da3f5ae
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
return
}

if (makeCopyrightCorrectYear(configuration.getCopyrightText()).isNotEmpty()) {
log.warn("Copyright year is not up do date.")
}

// need to make sure that copyright year is consistent with current year
val copyrightText = configuration.getCopyrightText()

if (makeCopyrightCorrectYear(copyrightText).isNotEmpty()) {
log.warn("Copyright year is not up do date.")
}

val headerComment = node.findChildBefore(PACKAGE_DIRECTIVE, BLOCK_COMMENT)
// Depends only on content and doesn't consider years
val isWrongCopyright = headerComment != null &&
Expand Down Expand Up @@ -234,11 +234,13 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
/**
* @return text of copyright as configured in the configuration file
*/
fun getCopyrightText() = config["copyrightText"] ?: error("Copyright is not set in configuration")
fun getCopyrightText() = config["copyrightText"]?.replace(CURR_YEAR_PATTERN, curYear.toString())
?: error("Copyright is not set in configuration")
}

companion object {
private val log = LoggerFactory.getLogger(HeaderCommentRule::class.java)
const val CURR_YEAR_PATTERN = ";@currYear;"
val hyphenRegex = Regex("""\d+-\d+""")
val afterCopyrightRegex = Regex("""((©|\([cC]\))+ *\d+)""")
val curYear = LocalDate.now().year
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ class HeaderCommentRuleFixTest : FixTestBase(
fixAndCompare("AutoCopyrightExpected.kt", "AutoCopyrightTest.kt")
}

@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.")
),
RulesConfig(HEADER_WRONG_FORMAT.name, true, emptyMap())))
}

/**
* Fixme there shouldn't be an additional blank line after copyright
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) {
RulesConfig("HEADER_MISSING_OR_WRONG_COPYRIGHT", true,
mapOf("copyrightText" to "Copyright (c) My Company, Ltd. 2012-$curYear. All rights reserved."))
)
private val rulesConfigListWithPattern: List<RulesConfig> = listOf(
RulesConfig("HEADER_MISSING_OR_WRONG_COPYRIGHT", true,
mapOf("copyrightText" to "Copyright (c) My Company, Ltd. 2012-;@currYear;. All rights reserved."))
)
private val rulesConfigListInvalidYear: List<RulesConfig> = listOf(
RulesConfig("HEADER_MISSING_OR_WRONG_COPYRIGHT", true,
mapOf("copyrightText" to "Copyright (c) My Company, Ltd. 2012-2019. All rights reserved."))
Expand All @@ -36,6 +40,10 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) {
RulesConfig("HEADER_MISSING_OR_WRONG_COPYRIGHT", true,
mapOf("copyrightText" to "Copyright (c) $curYear My Company, Ltd. All rights reserved."))
)
private val rulesConfigListYearWithPattern: List<RulesConfig> = listOf(
RulesConfig("HEADER_MISSING_OR_WRONG_COPYRIGHT", true,
mapOf("copyrightText" to "Copyright (c) ;@currYear; My Company, Ltd. All rights reserved."))
)
private val rulesConfigListCn: List<RulesConfig> = listOf(
RulesConfig("HEADER_MISSING_OR_WRONG_COPYRIGHT", true,
mapOf("copyrightText" to "版权所有 (c) 华为技术有限公司 2012-$curYear"))
Expand Down Expand Up @@ -207,6 +215,52 @@ class HeaderCommentRuleTest : LintTestBase(::HeaderCommentRule) {
)
}

@Test
@Tag(WarningNames.WRONG_COPYRIGHT_YEAR)
fun `copyright year good 3 - apply pattern`() {
lintMethod(
"""
/*
* Copyright (c) 2021 My Company, Ltd. All rights reserved.
*/
/**
* Very useful description, why this file has two classes
* foo bar baz
*/
package org.cqfn.diktat.example
class Example1 { }
class Example2 { }
""".trimIndent(),
rulesConfigList = rulesConfigListYearWithPattern
)
}

@Test
@Tag(WarningNames.WRONG_COPYRIGHT_YEAR)
fun `copyright year good 4 - apply pattern`() {
lintMethod(
"""
/*
* Copyright (c) My Company, Ltd. 2012-2021. All rights reserved.
*/
/**
* Very useful description, why this file has two classes
* foo bar baz
*/
package org.cqfn.diktat.example
class Example1 { }
class Example2 { }
""".trimIndent(),
rulesConfigList = rulesConfigListWithPattern
)
}

@Test
@Tag(WarningNames.WRONG_COPYRIGHT_YEAR)
fun `copyright year bad`() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
*/

package test.paragraph2.header

class Example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package test.paragraph2.header

class Example
2 changes: 1 addition & 1 deletion info/available-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
| 2 | 2.2.1 | KDOC_CONTAINS_DATE_OR_AUTHOR | Check: warns if header KDoc contains `@author` tag.<br>Warns if `@since` tag contains version and not date. | no | no | Detect author by other patterns (e.g. 'created by' etc.)|
| 2 | 2.3.1 | KDOC_TRIVIAL_KDOC_ON_FUNCTION | Check: warns if KDoc contains single line with words 'return', 'get' or 'set' | no | no | |
| 2 | 2.2.1 | HEADER_WRONG_FORMAT | Checks: warns if there is no newline after header KDoc<br>Fix: adds newline | yes | no | Check if header is on the very top of file. It's hard to determine when it's not.|
| 2 | 2.2.1 | HEADER_MISSING_OR_WRONG_COPYRIGHT | Checks: copyright exists on top of file and is properly formatted (as a block comment)<br>Fix: adds copyright if it is missing and required | yes | mandatoryCopyright | Allow setting copyright patterns via configuration|
| 2 | 2.2.1 | HEADER_MISSING_OR_WRONG_COPYRIGHT | Checks: copyright exists on top of file and is properly formatted (as a block comment)<br>Fix: adds copyright if it is missing and required | yes | isCopyrightMandatory, copyrightText (sets the copyright pattern for your project, you also can use `;@currYear;` key word in your text in aim to indicate current year, instead of explicit specifying) | |
| 2 | 2.2.1 | WRONG_COPYRIGHT_YEAR | Checks: copyright have a valid year<br>Fix: makes a year valid | yes | no | - |
| 2 | 2.2.1 | HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE | Check: warns if file with zero or >1 classes doesn't have header KDoc | no | no | |
| 2 | 2.4.2 | COMMENTED_OUT_CODE | Check: warns if commented code is detected (when un-commented, can be parsed) | no | no | Offset is lost when joined EOL comments are split again|
Expand Down

0 comments on commit da3f5ae

Please sign in to comment.