diff options
| author | Elena Vilchik | 2020-01-03 09:18:10 +0100 |
|---|---|---|
| committer | Christophe Zürn | 2020-01-03 09:18:10 +0100 |
| commit | 6258d923a5f42e00ddaf0ed45271933b2294b2fc (patch) | |
| tree | 6a1c07fc2940ed611072e36f500d01cb35a2b2d3 | |
| parent | 7bb9065e87f97dc41d6d22e8d3cb7741f6d08998 (diff) | |
| download | sonar-css-6258d923a5f42e00ddaf0ed45271933b2294b2fc.tar.bz2 | |
Rely on run-node on Mac (#236)
19 files changed, 319 insertions, 4 deletions
diff --git a/its/plugin/projects/(dir with paren)/src/file1.css b/its/plugin/projects/(dir with paren)/src/file1.css new file mode 100644 index 0000000..73d9f7e --- /dev/null +++ b/its/plugin/projects/(dir with paren)/src/file1.css @@ -0,0 +1,2 @@ +@import "a.css"; +@import "a.css"; /* S1128 | no-duplicate-at-import-rules */ diff --git a/its/plugin/src/test/java/org/sonar/css/its/NonStandardPathTest.java b/its/plugin/src/test/java/org/sonar/css/its/NonStandardPathTest.java new file mode 100644 index 0000000..5fc1793 --- /dev/null +++ b/its/plugin/src/test/java/org/sonar/css/its/NonStandardPathTest.java @@ -0,0 +1,75 @@ +/* + * SonarCSS + * Copyright (C) 2018-2019 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.css.its; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.SonarScanner; +import com.sonar.orchestrator.locator.FileLocation; +import java.io.File; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.sonarqube.ws.Issues.Issue; +import org.sonarqube.ws.client.issues.SearchRequest; +import org.sonarsource.analyzer.commons.ProfileGenerator; +import org.sonarsource.analyzer.commons.ProfileGenerator.RulesConfiguration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; +import static org.sonar.css.its.Tests.newWsClient; + +public class NonStandardPathTest { + + private static String PROJECT_KEY = "dir-with-paren"; + + @ClassRule + public static Orchestrator orchestrator = Tests.ORCHESTRATOR; + + @BeforeClass + public static void prepare() { + RulesConfiguration rulesConfiguration = new RulesConfiguration(); + File profile = ProfileGenerator.generateProfile(orchestrator.getServer().getUrl(), "css", "css", rulesConfiguration, Collections.emptySet()); + orchestrator.getServer().restoreProfile(FileLocation.of(profile)); + + orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY); + orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "css", "rules"); + + File projectDir = FileLocation.of("projects" + File.separator + "(dir with paren)").getFile(); + SonarScanner scanner = Tests.createScanner(PROJECT_KEY, projectDir); + orchestrator.executeBuild(scanner); + } + + @Test + public void test() { + SearchRequest request = new SearchRequest(); + request.setComponentKeys(Collections.singletonList(PROJECT_KEY)); + List<Issue> issuesList = newWsClient().issues().search(request).getIssuesList().stream() + .filter(i -> i.getRule().startsWith("css:")) + .collect(Collectors.toList()); + + assertThat(issuesList).extracting(Issue::getRule, Issue::getComponent).containsExactly( + tuple("css:S1128", "dir-with-paren:src/file1.css") + ); + } + +} diff --git a/its/plugin/src/test/java/org/sonar/css/its/Tests.java b/its/plugin/src/test/java/org/sonar/css/its/Tests.java index d3e62c7..eeb35a0 100644 --- a/its/plugin/src/test/java/org/sonar/css/its/Tests.java +++ b/its/plugin/src/test/java/org/sonar/css/its/Tests.java @@ -43,7 +43,8 @@ import org.sonarqube.ws.client.measures.ComponentRequest; IssuesTest.class, NoCssFileProjectTest.class, StylelintReportTest.class, - MinifiedTest.class + MinifiedTest.class, + NonStandardPathTest.class }) public class Tests { @@ -86,7 +87,10 @@ public class Tests { public static SonarScanner createScanner(String projectKey) { File projectDir = FileLocation.of("projects" + File.separator + projectKey).getFile(); + return createScanner(projectKey, projectDir); + } + public static SonarScanner createScanner(String projectKey, File projectDir) { return SonarScanner.create() .setSourceEncoding("UTF-8") .setProjectDir(projectDir) @@ -132,7 +132,7 @@ <dependency> <groupId>org.sonarsource.javascript</groupId> <artifactId>nodejs-utils</artifactId> - <version>5.1.1.7506</version> + <version>6.1.0.11503</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> diff --git a/sonar-css-plugin/css-bundle/package/.gitignore b/sonar-css-plugin/css-bundle/package/.gitignore new file mode 100644 index 0000000..7d683f0 --- /dev/null +++ b/sonar-css-plugin/css-bundle/package/.gitignore @@ -0,0 +1,2 @@ +!node_modules/ + diff --git a/sonar-css-plugin/css-bundle/package/node_modules/.bin/run-node b/sonar-css-plugin/css-bundle/package/node_modules/.bin/run-node new file mode 120000 index 0000000..861361e --- /dev/null +++ b/sonar-css-plugin/css-bundle/package/node_modules/.bin/run-node @@ -0,0 +1 @@ +../run-node/run-node
\ No newline at end of file diff --git a/sonar-css-plugin/css-bundle/package/node_modules/.yarn-integrity b/sonar-css-plugin/css-bundle/package/node_modules/.yarn-integrity new file mode 100644 index 0000000..f514a46 --- /dev/null +++ b/sonar-css-plugin/css-bundle/package/node_modules/.yarn-integrity @@ -0,0 +1,16 @@ +{ + "systemParams": "darwin-x64-57", + "modulesFolders": [ + "node_modules" + ], + "flags": [], + "linkedModules": [], + "topLevelPatterns": [ + "run-node@1.0.0" + ], + "lockfileEntries": { + "run-node@1.0.0": "https://registry.yarnpkg.com/run-node/-/run-node-1.0.0.tgz#46b50b946a2aa2d4947ae1d886e9856fd9cabe5e" + }, + "files": [], + "artifacts": {} +}
\ No newline at end of file diff --git a/sonar-css-plugin/css-bundle/package/node_modules/run-node/license b/sonar-css-plugin/css-bundle/package/node_modules/run-node/license new file mode 100644 index 0000000..e7af2f7 --- /dev/null +++ b/sonar-css-plugin/css-bundle/package/node_modules/run-node/license @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/sonar-css-plugin/css-bundle/package/node_modules/run-node/package.json b/sonar-css-plugin/css-bundle/package/node_modules/run-node/package.json new file mode 100644 index 0000000..1cb6c90 --- /dev/null +++ b/sonar-css-plugin/css-bundle/package/node_modules/run-node/package.json @@ -0,0 +1,39 @@ +{ + "name": "run-node", + "version": "1.0.0", + "description": "Run the Node.js binary no matter what", + "license": "MIT", + "repository": "sindresorhus/run-node", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "bin": "run-node", + "engines": { + "node": ">=4" + }, + "scripts": { + "test": "./run-node --version" + }, + "files": [ + "run-node" + ], + "keywords": [ + "run", + "node", + "nodejs", + "node.js", + "find", + "binary", + "bin", + "execute", + "which", + "detect", + "path", + "env", + "bash", + "shell", + "sh" + ] +} diff --git a/sonar-css-plugin/css-bundle/package/node_modules/run-node/readme.md b/sonar-css-plugin/css-bundle/package/node_modules/run-node/readme.md new file mode 100644 index 0000000..2ba18e1 --- /dev/null +++ b/sonar-css-plugin/css-bundle/package/node_modules/run-node/readme.md @@ -0,0 +1,70 @@ +# run-node [](https://travis-ci.org/sindresorhus/run-node) + +> Run the Node.js binary no matter what + +You can't always assume running `$ node file.js` will just work. The user might have the `node` binary in a non-standard location. They might be using a Node.js version manager like `nvm`, which is sourced in a subshell and not available from the outside. It also depends from where you're trying to run it. For example, GUI apps on macOS doesn't inherit the [`$PATH`](https://en.wikipedia.org/wiki/PATH_(variable)), so the `node` binary would not be found. Most projects that depend on Node.js just end up telling the user to manually set the full path to the `node` binary in some project specific settings. Now every project has to do this. [Ugh...](https://gist.github.com/cookrn/4015437) I prefer things to *just* work. With this module it will. + +This Bash script uses some tricks to find the Node.js binary on your system and run it. + +Can be used from any environment that can spawn a process (Shell, Python, Ruby, Swift, Objective-C, etc). + + +### npm + +#### Install + +``` +$ npm install run-node +``` + +#### Usage + +``` +$ ./node_modules/.bin/run-node file.js +``` + +Or in an [npm run script](https://docs.npmjs.com/cli/run-script): + +```json +{ + "start": "run-node file.js" +} +``` + +### Manually + +#### Install + +Download the [run-node](run-node) file: + +``` +$ curl -sSLO https://github.com/sindresorhus/run-node/raw/master/run-node && chmod +x run-node +``` + +#### Usage + +``` +./run-node file.js +``` + +#### Customizable cache path and error message + +The cache path and error message are defined by the `RUN_NODE_CACHE_PATH` and `RUN_NODE_ERROR_MSG` environment variables. You could use them in a script or add them to your `~.bashrc`. + +Default config: + +```sh +export RUN_NODE_ERROR_MSG="Couldn't find the Node.js binary. Ensure you have Node.js installed. Open an issue on https://github.com/sindresorhus/run-node" +export RUN_NODE_CACHE_PATH="/home/username/.node_path" +``` + + +## Created by + +- [Sindre Sorhus](https://github.com/sindresorhus) +- [Mathias Fredriksson](https://github.com/mafredri) + + +## License + +MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/sonar-css-plugin/css-bundle/package/node_modules/run-node/run-node b/sonar-css-plugin/css-bundle/package/node_modules/run-node/run-node new file mode 100755 index 0000000..2b7ce4c --- /dev/null +++ b/sonar-css-plugin/css-bundle/package/node_modules/run-node/run-node @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# MIT License © Sindre Sorhus + +if [[ -z $RUN_NODE_CACHE_PATH ]]; then + PATH_CACHE="$HOME"/.node_path +else + PATH_CACHE="$RUN_NODE_CACHE_PATH" +fi + +get_user_path() { + [[ -x "/usr/libexec/path_helper" ]] && eval $(/usr/libexec/path_helper -s) + echo "$($SHELL -i -l -c 'echo -e "\n"PATH=\"$PATH:\$PATH\""\n"' 2>/dev/null | grep "^PATH=")" > "$PATH_CACHE" +} + +set_path() { + if [[ -f "$PATH_CACHE" ]]; then + . "$PATH_CACHE" + else + get_user_path + . "$PATH_CACHE" + fi + + export PATH +} + +has_node() { + command -v node >/dev/null 2>&1 +} + +# Check if we have node, otherwise inherit path from user shell +if ! has_node; then + set_path + + # Retry by deleting old path cache + if ! has_node; then + rm "$PATH_CACHE" + set_path + fi +fi + +if has_node; then + node "$@" +else + if [[ -z $RUN_NODE_ERROR_MSG ]]; then + echo "Couldn't find the Node.js binary. Ensure you have Node.js installed. Open an issue on https://github.com/sindresorhus/run-node" + else + echo "$RUN_NODE_ERROR_MSG" + fi +fi diff --git a/sonar-css-plugin/css-bundle/package/package.json b/sonar-css-plugin/css-bundle/package/package.json new file mode 100644 index 0000000..e65c6ac --- /dev/null +++ b/sonar-css-plugin/css-bundle/package/package.json @@ -0,0 +1,11 @@ +{ + "name": "run-node-bundle", + "version": "1.0.0", + "description": "Small nested project to provide run-node", + "main": "index.js", + "author": "", + "license": "LGPL-3.0", + "dependencies": { + "run-node": "1.0.0" + } +} diff --git a/sonar-css-plugin/css-bundle/package/yarn.lock b/sonar-css-plugin/css-bundle/package/yarn.lock new file mode 100644 index 0000000..9675ef6 --- /dev/null +++ b/sonar-css-plugin/css-bundle/package/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +run-node@1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/run-node/-/run-node-1.0.0.tgz#46b50b946a2aa2d4947ae1d886e9856fd9cabe5e" + integrity sha512-kc120TBlQ3mih1LSzdAJXo4xn/GWS2ec0l3S+syHDXP9uRr0JAT8Qd3mdMuyjqCzeZktgP3try92cEgf9Nks8A== diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/server/CssAnalyzerBridgeServer.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/server/CssAnalyzerBridgeServer.java index 702e880..a7bc764 100644 --- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/server/CssAnalyzerBridgeServer.java +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/server/CssAnalyzerBridgeServer.java @@ -108,7 +108,7 @@ public class CssAnalyzerBridgeServer { PROFILER.stopDebug(); } - private void initNodeCommand(SensorContext context, File scriptFile) { + private void initNodeCommand(SensorContext context, File scriptFile) throws IOException { nodeCommandBuilder .outputConsumer(message -> { if (message.startsWith("DEBUG")) { @@ -122,6 +122,7 @@ public class CssAnalyzerBridgeServer { .minNodeVersion(8) .configuration(context.config()) .script(scriptFile.getAbsolutePath()) + .pathResolver(bundle) .scriptArgs(String.valueOf(port)); context.config() diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/server/bundle/Bundle.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/server/bundle/Bundle.java index 2639500..0ffd46d 100644 --- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/server/bundle/Bundle.java +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/server/bundle/Bundle.java @@ -20,8 +20,9 @@ package org.sonar.css.plugin.server.bundle; import java.nio.file.Path; +import org.sonarsource.nodejs.BundlePathResolver; -public interface Bundle { +public interface Bundle extends BundlePathResolver { void deploy(Path deployLocation); diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/server/bundle/CssAnalyzerBundle.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/server/bundle/CssAnalyzerBundle.java index eb59f05..7439ac1 100644 --- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/server/bundle/CssAnalyzerBundle.java +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/server/bundle/CssAnalyzerBundle.java @@ -45,6 +45,7 @@ public class CssAnalyzerBundle implements Bundle { final String bundleLocation; private String startServerScript = DEFAULT_STARTUP_SCRIPT.toString(); + private Path deployLocation; public CssAnalyzerBundle() { this(DEFAULT_BUNDLE_LOCATION); @@ -57,6 +58,7 @@ public class CssAnalyzerBundle implements Bundle { @Override public void deploy(Path deployLocation) { + this.deployLocation = deployLocation; PROFILER.startDebug("Deploying bundle"); LOG.debug("Deploying css-bundle into {}", deployLocation); InputStream bundle = getClass().getResourceAsStream(bundleLocation); @@ -78,4 +80,8 @@ public class CssAnalyzerBundle implements Bundle { return startServerScript; } + @Override + public String resolve(String relativePath) { + return deployLocation.resolve("css-bundle").resolve(relativePath).toString(); + } } diff --git a/sonar-css-plugin/src/sonarcss-assembly.xml b/sonar-css-plugin/src/sonarcss-assembly.xml index 3bd1f9a..e056267 100644 --- a/sonar-css-plugin/src/sonarcss-assembly.xml +++ b/sonar-css-plugin/src/sonarcss-assembly.xml @@ -11,6 +11,8 @@ <directory>css-bundle</directory> <includes> <include>lib/**/*</include> + <!-- contains 'run-node', default NodeJS on MacOS --> + <include>package/**/*</include> <include>bin/**/*</include> <include>node_modules/**/*</include> </includes> diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/server/CssAnalyzerBridgeServerTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/server/CssAnalyzerBridgeServerTest.java index 29b6bd6..d0a2c2f 100644 --- a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/server/CssAnalyzerBridgeServerTest.java +++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/server/CssAnalyzerBridgeServerTest.java @@ -19,6 +19,7 @@ */ package org.sonar.css.plugin.server; +import java.io.File; import java.nio.file.Path; import org.junit.After; import org.junit.Before; @@ -254,5 +255,11 @@ public class CssAnalyzerBridgeServerTest { public String startServerScript() { return "src/test/resources/mock-start-server/" + startServerScript; } + + @Override + public String resolve(String relativePath) { + File file = new File("src/test/resources"); + return new File(file.getAbsoluteFile(), relativePath).getAbsolutePath(); + } } } diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/server/bundle/CssAnalyzerBundleTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/server/bundle/CssAnalyzerBundleTest.java index 96805d1..700b656 100644 --- a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/server/bundle/CssAnalyzerBundleTest.java +++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/server/bundle/CssAnalyzerBundleTest.java @@ -82,4 +82,16 @@ public class CssAnalyzerBundleTest { bundle.deploy(deployLocation); }).doesNotThrowAnyException(); } + + @Test + public void test_resolve() { + Bundle bundle = new CssAnalyzerBundle("/bundle/test-css-bundle.zip"); + Path deployLocation = tempFolder.newDir().toPath(); + bundle.deploy(deployLocation); + assertThat(bundle.resolve("relative/path")) + .contains("css-bundle") + .endsWith("path") + .startsWith(deployLocation.toString()); + + } } |
