如何为基于 Typescript 的 AngularJS 项目配置 SonarQube?
Posted
技术标签:
【中文标题】如何为基于 Typescript 的 AngularJS 项目配置 SonarQube?【英文标题】:How to configure SonarQube for Typescript based AngularJS project? 【发布时间】:2019-08-13 05:16:21 【问题描述】:我已经设置了 SonarQube 和 SonarScanner 并设法扫描了我的项目。除了代码覆盖率(始终为 0%)外,我可以在屏幕上看到其他指标。我错过了什么?
#----- Default SonarQube server
sonar.host.url=http://localhost:9000/sonar
#----- Default source code encoding
#sonar.sourceEncoding=UTF-8
# must be unique in a given SonarQube instance
sonar.projectKey=pwta
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=PWTA
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=client
sonar.language=ts
sonar.exclusions=**/node_modules/**
sonar.typescript.lcov.reportPaths=coverage/lcov.info
以上是 sonar-project.properties 文件的摘录。而位于 coverage/lcov.info 的 LCOV 文件是使用 nyc 库生成的。
来自命令提示符的日志:
INFO: Scanner configuration file: NONE
INFO: Project root configuration file: C:\Repos\pwta-desktop\ui\sonar-project.properties
INFO: SonarQube Scanner 3.3.0.1492
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Windows 10 10.0 amd64
INFO: User cache: C:\Users\koklliew\.sonar\cache
INFO: SonarQube server 7.6.0
INFO: Default locale: "en_US", source code encoding: "windows-1252" (analysis is platform dependent)
INFO: Load global settings
INFO: Load global settings (done) | time=90ms
INFO: Server id: 9CFC3560-AWmQJqPmStMXNHgIIeVs
INFO: User cache: C:\Users\koklliew\.sonar\cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=166ms
INFO: Load/download plugins (done) | time=223ms
INFO: Process project properties
INFO: Execute project builders
INFO: Execute project builders (done) | time=4ms
INFO: Project key: pwta
INFO: Base dir: C:\Repos\pwta-desktop\ui
INFO: Working dir: C:\Repos\pwta-desktop\ui\.scannerwork
INFO: Load project settings
INFO: Load project settings (done) | time=49ms
INFO: Load project repositories
INFO: Load project repositories (done) | time=156ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=46ms
INFO: Load active rules
INFO: Load active rules (done) | time=802ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=36ms
INFO: Language is forced to ts
INFO: Indexing files...
INFO: Project configuration:
INFO: Excluded sources: **/node_modules/**, *.spec.ts
INFO: Included tests: *.spec.ts
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\app.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\app.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\backend\database\seeders\guiSettings.json' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\display-settings\display-settings-group\display-settings-group.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\display-settings\display-settings-tabs\display-settings-tabs.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\display-settings\display-settings.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\display-settings\display-settings.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\external-params\external-params-editor\external-params-editor.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\external-params\external-params-editor\external-params-editor.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\external-params\external-params.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\external-params\external-params.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\external-params\merge-external-params\merge-external-params.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\external-params\merge-external-params\merge-external-params.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\external-params\_external-params-global.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\external-params\_mixins.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\external-params\_variables.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\hidden-elements\hidden-elements.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\hidden-elements\hidden-elements.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\incompatible-testPlan-dialog\incompatible-testPlan-dialog.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\incompatible-testPlan-dialog\incompatible-testPlan-dialog.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\interaction-dialog\interaction-dialog.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\interaction-dialog\interaction-dialog.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\menu\menu.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\menu\menu.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\navigation-bar\navigation-bar.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\navigation-bar\navigation-bar.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\notification-area\notification-area.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\notification-area\notification-area.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\notifications-resource\notifications-resource.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\notifications-resource\notifications-resource.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\notifications-resource\overlay\overlay.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\notifications-resource\overlay\overlay.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\notifications-toolbar\filter\notification-filter.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\notifications-toolbar\filter\notification-filter.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\notifications-toolbar\notifications-toolbar.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\notifications-toolbar\notifications-toolbar.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\notifications-toolbar\search\notifications-search.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\notifications-toolbar\search\notifications-search.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\package-manager-dialog\directives\pm-extended.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\package-manager-dialog\package-manager-dialog.component.html' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\package-manager-dialog\package-manager-dialog.component.scss' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\property-editor\DisplayForProps.tsd.json' is ignored because it doesn't belong to the forced language 'ts'
WARN: File 'C:\Repos\pwta-desktop\ui\client\app\components\property-editor\DisplayForPropsTree.json' is ignored because it doesn't belong to the forced language 'ts'
...继续...
INFO: 343 files indexed
INFO: 0 files ignored because of inclusion/exclusion patterns
INFO: Quality profile for ts: Sonar way
INFO: ------------- Run sensors on module PWTA
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=12ms
INFO: Sensor JavaXmlSensor [java]
INFO: Sensor JavaXmlSensor [java] (done) | time=9ms
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=20ms
INFO: Sensor SonarTS [typescript]
INFO: Analyzing 343 typescript file(s) with the following configuration file C:\Repos\pwta-desktop\ui\tsconfig.json
INFO: 145 files analyzed out of 343. Current file: C:\Repos\pwta-desktop\ui\client\app\components\sequence-viewer\services\results-utility.service.spec.ts
INFO: 343 files analyzed out of 343
INFO: Sensor SonarTS [typescript] (done) | time=38926ms
INFO: Sensor SonarTS Coverage [typescript]
INFO: Analysing [C:\Repos\pwta-desktop\ui\coverage\lcov.info]
WARN: Could not resolve 1 file paths in [C:\Repos\pwta-desktop\ui\coverage\lcov.info], first unresolved path: c:\repos\pwta-desktop\ui\karma.conf.js
INFO: Sensor SonarTS Coverage [typescript] (done) | time=14ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=248ms
INFO: ------------- Run sensors on project
INFO: SCM provider for this project is: git
INFO: 1 files to be analyzed
INFO: 0/1 files analyzed
WARN: Missing blame information for the following files:
WARN: * client/version.ts
WARN: This may lead to missing/broken features in SonarQube
INFO: 43 files had no CPD blocks
INFO: Calculating CPD for 300 files
INFO: CPD calculation finished
INFO: Analysis report generated in 3904ms, dir size=2 MB
INFO: Analysis report compressed in 3421ms, zip size=1 MB
INFO: Analysis report uploaded in 145ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/sonar/dashboard?id=pwta
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://localhost:9000/sonar/api/ce/task?id=AWmkhBxOhYEp7lx6Lmb2
INFO: Analysis total time: 1:01.823 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 1:04.060s
INFO: Final Memory: 19M/407M
INFO: ------------------------------------------------------------------------
日志显示可以找到lcov文件。
而且,似乎在上面的日志中提到了有关路径的投诉。会不会是这个问题?
【问题讨论】:
【参考方案1】:我认为您的 sonar-project.properties 中的 lcov.info 路径有误。这是我的:
# TypeScript specific config
sonar.typescript.lcov.reportPaths=coverage/my-project/lcov.info
sonar.test.inclusions=**/*.spec.ts
【讨论】:
就我个人而言,我在 SonarQube Community EditionVersion 7.9.2 中使用了sonar.javascript.lcov.reportPaths
密钥(type
与 java
)
与sonar.typescript.lcov.reportPaths
我得到了WARN: The use of sonar.typescript.lcov.reportPaths for coverage import is deprecated, use sonar.javascript.lcov.reportPaths instead.
以上是关于如何为基于 Typescript 的 AngularJS 项目配置 SonarQube?的主要内容,如果未能解决你的问题,请参考以下文章
如何为 TypeScript 配置 Sublime 构建系统
如何为 TypeScript 配置 `*.graphql` 导入?
如何为 React Native 嵌套组件创建 Typescript 定义
如何为属性创建 TypeScript @enumerable(false) 装饰器
如何为以下 contextapi 代码的 useReducer useContext 设置 Typescript 类型?