Skip to content

Commit

Permalink
Merge pull request #436 from joesondow/custom-codenarc-rule-consecuti…
Browse files Browse the repository at this point in the history
…ve-blank-lines

Custom codenarc rule ConsecutiveBlankLines
  • Loading branch information
joesondow committed Dec 6, 2013
2 parents 6416c93 + 7f5ca5a commit 103012a
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 9 deletions.
6 changes: 5 additions & 1 deletion grails-app/conf/CodeNarcRuleSet.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ ruleset {
// description = 'Use of Spring.autowire(...) can become unstable.'
// }

rule('file:test/unit/com/netflix/asgard/codenarc/ConsecutiveBlankLinesRule.groovy') {
description = 'Consecutive blank lines are not permitted.'
}

// rulesets/basic.xml
AssertWithinFinallyBlock
AssignmentInConditional
Expand Down Expand Up @@ -163,7 +167,7 @@ ruleset {
BracesForIfElse
BracesForMethod
BracesForTryCatchFinally
ClassJavadoc
// ClassJavadoc
// LineLength
SpaceAfterCatch
// SpaceAfterClosingBrace
Expand Down
20 changes: 20 additions & 0 deletions scripts/Codenarc.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@
import grails.util.GrailsUtil
import org.apache.tools.ant.BuildException

// Begin hack to replace CodeNarc 0.19's implementation of loadRuleScriptFile which blows up in recent Groovy.
// Remove this hack after https://github.com/CodeNarc/CodeNarc/pull/26 gets merged and released
// in future CodeNarc version to fix https://github.com/CodeNarc/CodeNarc/issues/25
import org.codenarc.ruleset.RuleSetUtil
import org.codenarc.util.io.DefaultResourceFactory
import org.codenarc.util.io.ResourceFactory

ResourceFactory RESOURCE_FACTORY = new DefaultResourceFactory()
RuleSetUtil.metaClass.'static'.loadRuleScriptFile = { String path ->
def inputStream = RESOURCE_FACTORY.getResource(path).inputStream
Class ruleClass
inputStream.withStream { InputStream input ->
GroovyClassLoader gcl = new GroovyClassLoader(getClass().classLoader)
ruleClass = gcl.parseClass(input.text)
}
RuleSetUtil.assertClassImplementsRuleInterface(ruleClass)
ruleClass.newInstance()
}
// End temporary hack for compatibility between CodeNarc 0.19 and Groovy 1.8.*

includeTargets << grailsScript('_GrailsCompile')

target('codenarc': 'Run CodeNarc') {
Expand Down
1 change: 0 additions & 1 deletion src/groovy/com/netflix/asgard/EntityType.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ import java.lang.reflect.Modifier
{ "${it.workflowType.name}-${it.workflowType.version}" as String })
static final EntityType<DomainInfo> workflowDomain = create('Workflow Domain', { it.name })


/**
* Create an EntityType with specific attributes
*
Expand Down
2 changes: 0 additions & 2 deletions src/groovy/com/netflix/asgard/Relationships.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -287,5 +287,3 @@ class Relationships {
parts
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ interface DeploymentActivities {
*/
AsgDeploymentNames getAsgDeploymentNames(UserContext userContext, String clusterName)


/**
* Creates the launch configuration for the next ASG in the cluster.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ class MetricNamespaces {

private final ImmutableMap<String, MetricNamespace> allNamespacesByName


/**
* Construct MetricNamespaces with specified custom namespace metrics and dimensions.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ scheduled actions #scheduleNames and suspended processes #processNames""")
autoScalingGroupName: 'service1-int-v008', minSize: 1, desiredCapacity: 2, maxSize: 3))
}


def 'should determine healthy ASG due to no instances'() {
awsAutoScalingService = Spy(AwsAutoScalingService)

Expand Down
2 changes: 0 additions & 2 deletions test/unit/com/netflix/asgard/FlowServiceUnitSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class FlowServiceUnitSpec extends Specification {
WorkflowClientFactory workflowClientFactory = new WorkflowClientFactory(Mock(AmazonSimpleWorkflow))
FlowService flowService = new FlowService(workflowClientFactory: workflowClientFactory)


def 'should get new workflow client'() {
UserContext userContext = new UserContext(username: 'rtargaryen')
Link link = new Link(type: EntityType.cluster, id: '123')
Expand Down Expand Up @@ -73,5 +72,4 @@ class FlowServiceUnitSpec extends Specification {
expect:
flowService.getManualActivityCompletionClient('123') instanceof ManualActivityCompletionClient
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2013 Netflix, Inc.
*
* 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 com.netflix.asgard.codenarc

import org.codenarc.rule.AbstractRule
import org.codenarc.source.SourceCode

/**
* Makes sure there are no consecutive lines that are either blank or whitespace only.
*/
class ConsecutiveBlankLinesRule extends AbstractRule {

String name = 'ConsecutiveBlankLines'
int priority = 3

/**
* Apply the rule to the given source, writing violations to the given list.
* @param sourceCode The source to check
* @param violations A list of Violations that may be added to. It can be an empty list
*/
@Override
void applyTo(SourceCode sourceCode, List violations) {

List<String> lines = sourceCode.getLines()
for (int index = 1; index < lines.size(); index++) {
String line = lines[index]
if (line.trim().isEmpty() && lines[index - 1].trim().isEmpty()) {
violations.add(createViolation(index, line, "File $sourceCode.name has consecutive blank lines"))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2013 Netflix, Inc.
*
* 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 com.netflix.asgard.codenarc

import org.codenarc.rule.AbstractRuleTestCase
import org.codenarc.rule.Rule
import org.junit.Test

@SuppressWarnings('ConsecutiveBlankLines')
class ConsecutiveBlankLinesRuleTest extends AbstractRuleTestCase {

@Test
void testRuleProperties() {
assert rule.priority == 3
assert rule.name == 'ConsecutiveBlankLines'
}

@Test
void testSuccessScenario() {
final SOURCE = '''
class MyClass {
def go() { /* ... */ }
def goSomewhere() { /* ... */ }
def build() { /* ... */ }
def buildSomething() { /* ... */ }
}
'''
assertNoViolations(SOURCE)
}

@Test
void testClassStartsWithDoubleBlankLines() {
final SOURCE = '''
class MyClass {
void go() { /* ... */ }
}
'''
assertSingleViolation(SOURCE, 3, '', "File null has consecutive blank lines")
}

@Test
void testFileStartsWithDoubleBlankLines() {
final SOURCE = '''
class MyClass {
void go() { /* ... */ }
}
'''
assertSingleViolation(SOURCE, 1, '', "File null has consecutive blank lines")
}

@Test
void testDoubleBlankLinesBetweenMethods() {
final SOURCE = '''
class MyClass {
void go() { /* ... */ }
void stop() { /* ... */ }
void run() { /* ... */ }
}
'''
assertTwoViolations(SOURCE, 4, '', 7, '')
}

@Test
void testTripleBlankLines() {
final SOURCE = '''
class MyClass {
void go() { /* ... */ }
void stop() { /* ... */ }
}
'''
assertTwoViolations(SOURCE, 4, '', 5, '')
}

protected Rule createRule() {
new ConsecutiveBlankLinesRule()
}
}

0 comments on commit 103012a

Please sign in to comment.