From 56a0283ee7cea418b6d53258073b9dffce6b1b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kalkosi=C5=84ski?= Date: Mon, 8 Jan 2018 09:59:49 +0100 Subject: [PATCH] Update JsHint to 2.9.5 (#175) --- build.gradle | 2 +- .../processor/jshint/JsHintProcessor.java | 28 +++---- .../pl/touk/sputnik/SputnikAssertions.java | 12 +++ .../sputnik/assertion/ViolationAssert.java | 32 ++++++++ .../processor/jshint/JsHintProcessorTest.java | 76 ++++++++++++------- .../processor/jslint/JsLintProcessorTest.java | 33 ++++---- src/test/resources/js/reducers.js | 22 ++++++ src/test/resources/js/test.js | 4 +- src/test/resources/jshint/jshint.json | 5 +- 9 files changed, 147 insertions(+), 67 deletions(-) create mode 100644 src/test/java/pl/touk/sputnik/SputnikAssertions.java create mode 100644 src/test/java/pl/touk/sputnik/assertion/ViolationAssert.java create mode 100644 src/test/resources/js/reducers.js diff --git a/build.gradle b/build.gradle index f9ad4ab3..39b7e482 100644 --- a/build.gradle +++ b/build.gradle @@ -114,7 +114,7 @@ dependencies { compile 'com.googlecode.jslint4java:jslint4java:2.0.5' // JSHint - compile 'pl.gildur:jshint4j:1.0.1' + compile 'pl.touk:jshint4j:2.9.5' // github connector compile 'com.jcabi:jcabi-github:0.24' diff --git a/src/main/java/pl/touk/sputnik/processor/jshint/JsHintProcessor.java b/src/main/java/pl/touk/sputnik/processor/jshint/JsHintProcessor.java index 7b38eb0a..9a89f071 100644 --- a/src/main/java/pl/touk/sputnik/processor/jshint/JsHintProcessor.java +++ b/src/main/java/pl/touk/sputnik/processor/jshint/JsHintProcessor.java @@ -1,28 +1,22 @@ package pl.touk.sputnik.processor.jshint; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.util.List; - +import com.google.common.base.Strings; +import com.google.common.io.CharStreams; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; -import pl.gildur.jshint4j.Error; -import pl.gildur.jshint4j.JsHint; +import pl.touk.jshint4j.Error; +import pl.touk.jshint4j.JsHint; import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.GeneralOption; -import pl.touk.sputnik.review.Review; -import pl.touk.sputnik.review.ReviewException; -import pl.touk.sputnik.review.ReviewProcessor; -import pl.touk.sputnik.review.ReviewResult; -import pl.touk.sputnik.review.Severity; -import pl.touk.sputnik.review.Violation; +import pl.touk.sputnik.review.*; import pl.touk.sputnik.review.filter.JavaScriptFilter; import pl.touk.sputnik.review.transformer.IOFileTransformer; -import com.google.common.base.Strings; -import com.google.common.io.CharStreams; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.List; @Slf4j @AllArgsConstructor @@ -70,13 +64,13 @@ private String readConfiguration() { throw new ReviewException("IO exception when reading JSHint configuration.", e); } } - + private String getConfigurationFileName() { String configurationFile = config.getProperty(GeneralOption.JSHINT_CONFIGURATION_FILE); log.info("Using JSHint configuration file {}", configurationFile); return configurationFile; } - + @Override public String getName() { return SOURCE_NAME; diff --git a/src/test/java/pl/touk/sputnik/SputnikAssertions.java b/src/test/java/pl/touk/sputnik/SputnikAssertions.java new file mode 100644 index 00000000..6bdd9cff --- /dev/null +++ b/src/test/java/pl/touk/sputnik/SputnikAssertions.java @@ -0,0 +1,12 @@ +package pl.touk.sputnik; + +import org.assertj.core.api.Assertions; +import pl.touk.sputnik.assertion.ViolationAssert; +import pl.touk.sputnik.review.Violation; + +public class SputnikAssertions extends Assertions { + + public static ViolationAssert assertThat(Violation actual) { + return new ViolationAssert(actual); + } +} diff --git a/src/test/java/pl/touk/sputnik/assertion/ViolationAssert.java b/src/test/java/pl/touk/sputnik/assertion/ViolationAssert.java new file mode 100644 index 00000000..0f6331ca --- /dev/null +++ b/src/test/java/pl/touk/sputnik/assertion/ViolationAssert.java @@ -0,0 +1,32 @@ +package pl.touk.sputnik.assertion; + +import org.assertj.core.api.AbstractAssert; +import pl.touk.sputnik.review.Severity; +import pl.touk.sputnik.review.Violation; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ViolationAssert extends AbstractAssert { + + public ViolationAssert(Violation actual) { + super(actual, ViolationAssert.class); + } + + public ViolationAssert hasLine(int line) { + isNotNull(); + assertThat(actual.getLine()).isEqualTo(line); + return this; + } + + public ViolationAssert hasMessage(String message) { + isNotNull(); + assertThat(actual.getMessage()).isEqualTo(message); + return this; + } + + public ViolationAssert hasSeverity(Severity severity) { + isNotNull(); + assertThat(actual.getSeverity()).isEqualTo(severity); + return this; + } +} \ No newline at end of file diff --git a/src/test/java/pl/touk/sputnik/processor/jshint/JsHintProcessorTest.java b/src/test/java/pl/touk/sputnik/processor/jshint/JsHintProcessorTest.java index b90e5bce..2a07d758 100644 --- a/src/test/java/pl/touk/sputnik/processor/jshint/JsHintProcessorTest.java +++ b/src/test/java/pl/touk/sputnik/processor/jshint/JsHintProcessorTest.java @@ -2,32 +2,20 @@ import com.google.common.collect.ImmutableList; import com.google.common.io.Resources; -import org.junit.Before; import org.junit.Test; import pl.touk.sputnik.configuration.Configuration; import pl.touk.sputnik.configuration.ConfigurationBuilder; -import pl.touk.sputnik.review.Review; -import pl.touk.sputnik.review.ReviewFile; -import pl.touk.sputnik.review.ReviewFormatterFactory; -import pl.touk.sputnik.review.ReviewResult; +import pl.touk.sputnik.review.*; -import static org.assertj.core.api.Assertions.assertThat; +import static pl.touk.sputnik.SputnikAssertions.assertThat; public class JsHintProcessorTest { - private JsHintProcessor fixture; - private Configuration config; - - @Before - public void setUp() throws Exception { - config = ConfigurationBuilder.initFromResource("jshint/sputnik/noConfigurationFile.properties"); - fixture = new JsHintProcessor(config); - } - - @Test public void shouldReturnEmptyResultWhenNoFilesToReview() { // given + Configuration config = ConfigurationBuilder.initFromResource("jshint/sputnik/noConfigurationFile.properties"); + JsHintProcessor fixture = new JsHintProcessor(config); Review review = new Review(ImmutableList.of(new ReviewFile("test")), ReviewFormatterFactory.get(config)); // when @@ -39,8 +27,10 @@ public void shouldReturnEmptyResultWhenNoFilesToReview() { } @Test - public void shouldReturnNoViolationsOnSimpleFunction() { + public void shouldReturnOneViolationWithConfigurationOnSimpleFunction() { // given + Configuration config = ConfigurationBuilder.initFromResource("jshint/sputnik/withConfigurationFile.properties"); + JsHintProcessor fixture = new JsHintProcessor(config); Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/test.js").getFile())), ReviewFormatterFactory.get(config)); // when @@ -48,26 +38,54 @@ public void shouldReturnNoViolationsOnSimpleFunction() { // then assertThat(reviewResult).isNotNull(); - assertThat(reviewResult.getViolations()).isEmpty(); + assertThat(reviewResult.getViolations()).hasSize(2); + assertThat(reviewResult.getViolations().get(0)).hasLine(2) + .hasMessage("Missing semicolon.") + .hasSeverity(Severity.INFO); + assertThat(reviewResult.getViolations().get(1)).hasLine(1) + .hasMessage("'test' is defined but never used.") + .hasSeverity(Severity.INFO); } @Test - public void shouldReturnOneViolationWithConfigurationOnSimpleFunction() { + public void shouldReturnViolationsWithConfigurationOnReducerExample() { // given - config = ConfigurationBuilder.initFromResource("jshint/sputnik/withConfigurationFile.properties"); - fixture = new JsHintProcessor(config); - Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/test.js").getFile())), ReviewFormatterFactory.get(config)); + Configuration config = ConfigurationBuilder.initFromResource("jshint/sputnik/withConfigurationFile.properties"); + JsHintProcessor fixture = new JsHintProcessor(config); + Review review = new Review(ImmutableList.of(new ReviewFile(Resources.getResource("js/reducers.js").getFile())), ReviewFormatterFactory.get(config)); // when ReviewResult reviewResult = fixture.process(review); // then assertThat(reviewResult).isNotNull(); - assertThat(reviewResult.getViolations()).hasSize(1); - assertThat(reviewResult.getViolations()) - .extracting("message") - .containsOnly( - "'alert' is not defined." - ); + assertThat(reviewResult.getViolations()).hasSize(9); + assertThat(reviewResult.getViolations().get(0)).hasLine(4) + .hasMessage("Missing semicolon.") + .hasSeverity(Severity.INFO); + assertThat(reviewResult.getViolations().get(1)).hasLine(10) + .hasMessage("Missing semicolon.") + .hasSeverity(Severity.INFO); + assertThat(reviewResult.getViolations().get(2)).hasLine(11) + .hasMessage("Missing semicolon.") + .hasSeverity(Severity.INFO); + assertThat(reviewResult.getViolations().get(3)).hasLine(13) + .hasMessage("Regular parameters should not come after default parameters.") + .hasSeverity(Severity.INFO); + assertThat(reviewResult.getViolations().get(4)).hasLine(16) + .hasMessage("Missing semicolon.") + .hasSeverity(Severity.INFO); + assertThat(reviewResult.getViolations().get(5)).hasLine(18) + .hasMessage("Missing semicolon.") + .hasSeverity(Severity.INFO); + assertThat(reviewResult.getViolations().get(6)).hasLine(4) + .hasMessage("'combineReducers' is defined but never used.") + .hasSeverity(Severity.INFO); + assertThat(reviewResult.getViolations().get(7)).hasLine(6) + .hasMessage("'ADD_TODO' is defined but never used.") + .hasSeverity(Severity.INFO); + assertThat(reviewResult.getViolations().get(8)).hasLine(7) + .hasMessage("'TOGGLE_TODO' is defined but never used.") + .hasSeverity(Severity.INFO); } -} +} \ No newline at end of file diff --git a/src/test/java/pl/touk/sputnik/processor/jslint/JsLintProcessorTest.java b/src/test/java/pl/touk/sputnik/processor/jslint/JsLintProcessorTest.java index cfa9f562..6547ab0e 100644 --- a/src/test/java/pl/touk/sputnik/processor/jslint/JsLintProcessorTest.java +++ b/src/test/java/pl/touk/sputnik/processor/jslint/JsLintProcessorTest.java @@ -6,12 +6,9 @@ import org.junit.Test; import pl.touk.sputnik.TestEnvironment; import pl.touk.sputnik.configuration.ConfigurationBuilder; -import pl.touk.sputnik.review.Review; -import pl.touk.sputnik.review.ReviewFile; -import pl.touk.sputnik.review.ReviewFormatterFactory; -import pl.touk.sputnik.review.ReviewResult; +import pl.touk.sputnik.review.*; -import static org.assertj.core.api.Assertions.assertThat; +import static pl.touk.sputnik.SputnikAssertions.assertThat; public class JsLintProcessorTest extends TestEnvironment { @@ -46,12 +43,13 @@ public void shouldReturnNoViolationsOnSimpleFunction() { // then assertThat(reviewResult).isNotNull(); - assertThat(reviewResult.getViolations()).hasSize(1); - assertThat(reviewResult.getViolations()) - .extracting("message") - .containsOnly( - "Missing 'use strict' statement." - ); + assertThat(reviewResult.getViolations()).hasSize(2); + assertThat(reviewResult.getViolations().get(0)).hasLine(2) + .hasMessage("Missing 'use strict' statement.") + .hasSeverity(Severity.INFO); + assertThat(reviewResult.getViolations().get(1)).hasLine(2) + .hasMessage("Expected ';' and instead saw '}'.") + .hasSeverity(Severity.INFO); } @Test @@ -65,11 +63,12 @@ public void shouldReturnOneViolationWithConfigurationOnSimpleFunction() { // then assertThat(reviewResult).isNotNull(); - assertThat(reviewResult.getViolations()).hasSize(1); - assertThat(reviewResult.getViolations()) - .extracting("message") - .containsOnly( - "Missing 'use strict' statement." - ); + assertThat(reviewResult.getViolations()).hasSize(2); + assertThat(reviewResult.getViolations().get(0)).hasLine(2) + .hasMessage("Missing 'use strict' statement.") + .hasSeverity(Severity.INFO); + assertThat(reviewResult.getViolations().get(1)).hasLine(2) + .hasMessage("Expected ';' and instead saw '}'.") + .hasSeverity(Severity.INFO); } } diff --git a/src/test/resources/js/reducers.js b/src/test/resources/js/reducers.js new file mode 100644 index 00000000..2d71ec87 --- /dev/null +++ b/src/test/resources/js/reducers.js @@ -0,0 +1,22 @@ +//Part of an original file copied from a Redux documentation: +//https://redux.js.org/docs/basics/Reducers.html + +import { combineReducers } from 'redux' +import { + ADD_TODO, + TOGGLE_TODO, + SET_VISIBILITY_FILTER, + VisibilityFilters +} from './actions' +const { SHOW_ALL } = VisibilityFilters + +function visibilityFilter(state = SHOW_ALL, action) { + switch (action.type) { + case SET_VISIBILITY_FILTER: + return action.filter + default: + return state + } +} + +export default visibilityFilter; \ No newline at end of file diff --git a/src/test/resources/js/test.js b/src/test/resources/js/test.js index 9b74115b..f66ef8d6 100644 --- a/src/test/resources/js/test.js +++ b/src/test/resources/js/test.js @@ -1,3 +1,3 @@ function test() { - alert('foo bar'); -} + alert('foo bar') +} \ No newline at end of file diff --git a/src/test/resources/jshint/jshint.json b/src/test/resources/jshint/jshint.json index ba44b497..f2c4b2a7 100644 --- a/src/test/resources/jshint/jshint.json +++ b/src/test/resources/jshint/jshint.json @@ -1,3 +1,6 @@ { - "undef": true + "undef": true, + "unused": true, + "devel": true, + "esversion": 6 } \ No newline at end of file