source: pyenvjasmine/pyenvjasmine/envjasmine/lib/jscover/README.textile@ 19:ab5f65372038

Last change on this file since 19:ab5f65372038 was 19:ab5f65372038, checked in by Borja Lopez <borja@…>, 9 years ago

Imported latest envjasmine version, the initial import from darcs had the wrong version

File size: 8.1 KB
Line 
1h1. JSCover and Sonar integration for EnvJasmine (beta)
2
3Allows you to pull code coverage metrics for EnvJasmine tests and view the results in Sonar.
4
5h2. 3rd Party Tools
6
7JSCover is a code coverage tool for javascript. See http://tntim96.github.com/JSCover/
8
9Sonar is a free, open source, code quality analytics tool. See http://www.sonarsource.org/
10
11Sonar has a plugin to report on javascript quality metrics. See http://docs.codehaus.org/display/SONAR/JavaScript+Plugin
12
13Maven (included example) or Sonar Runner (default example). See http://maven.apache.org/ or http://docs.codehaus.org/display/SONAR/Installing+and+Configuring+Sonar+Runner
14
15h2. Prerequisites
16
17This readme assumes:
18
19you already familiar with Sonar,
20you are already the concept of code coverage,
21you have maven or Sonar Runner installed (depending on which example you plan to run)
22
23h2. Running The example
24
25To calculate and report code coverage metrics to Sonar:
26
27Install Sonar.
28Install Sonar-Runner.
29Install the Sonar javascript plugin.
30Run your local Sonar server.
31In @include/dependencies.js@, uncomment @EnvJasmine.loadGlobal(EnvJasmine.libDir + "/jscover/envjasmine-sonar-coverage-properties.js");@
32In @include/dependencies.js@, uncomment @EnvJasmine.loadGlobal(EnvJasmine.coverage.envjasmine_coverage_js);@
33Execute @bin/run_coverage.sh --sonarMethod=sonar-runner@
34Find the project in Sonar and add the code coverage widget to your dashboard.
35
36h2. How to configure for use in your project
37
38h3. dependencies.js
39
40In your @dependencies.js@ uncomment the 2 lines loading the coverage properties file and the coverage plugin.
41
42h3. command line options
43
44projectRoot - the root folder of your project.
45originalDir - the root folder of your javascript.
46instrumentedDir - the folder you want your javascript copied to.
47reportsDir - the output directory for sonar reports.
48sonarMethod - "sonar-runner" or "maven" depending on which method you use. Or "none" if you are going to run sonar manually.
49noInstrument - files you want excluded from instrumentation (such as jquery). This can be set multiple times. This path is relative to the originalDir.
50debug - will output extra information to the console.
51cleanup - will delete the reports and instrumentation directory after completion.
52
53h3. sonar.js.xml (if you use maven)
54
55This must stay in the root directory.
56The "modelVersion" property must be compatible with your version of maven.
57"sourceDirectory" should be set to the root javascript directory.
58"sonar.exclusions" will exclude files from sonar analytics. "*" and "**" wildcards can be used.
59"sonar.javascript.testframework" must not be edited
60"sonar.javascript.jstestdriver.reportsfolder" should be set to your temp reports folder.
61All other properties are identifying labels and can be edited as long as no other process relies on them.
62
63h3. sonar-project.properties (if you use sonar-runner)
64
65Check out the documentation at http://docs.codehaus.org/display/SONAR/Installing+and+Configuring+Sonar+Runner
66
67h2. File overview
68
69@bin/run_coverage.sh@ is the main files which kick off the process.
70@include/dependencies.js@ loads EnvJasmine dependencies including the coverage plugin.
71@lib/envjasmine.js@ is the same EnvJasmine file you're been using to run your EnvJasmine tests.
72@lib/jscover/envjasmine-sonar-coverage-runner.js@ is the main execution script called by run_coverage.
73@lib/jscover/envjasmine-sonar-coverage-properties.js@ contains almost of the variables you will need to set to configure your setup.
74@lib/jscover/envjasmine-sonar-coverage-helper.js@ contains helper functions.
75@lib/jscover/envjasmine-coverage.js@ is the EnvJasmine coverage plugin. This adds itself to EnvJasmine's post test functions and writes the coverage data to disk.
76@lib/jscover/JSCover-all.jar@ is a copy of the JSCover jar.
77@samples/notCoveredDemo.js@ and @specs/notCoveredDemo.js@ are files that demo coverage gaps.
78@sonar.js.xml@ is the minimal pom file needed to get data into sonar if you use maven. Unfortunately, this needs to be in the project root.
79@sonar-project.properties@ is the config file for sonar-runner.
80
81h2. Detailed discussion of envjasmine-sonar-coverage-runner.js
82
83h3. Initialization
84
85This script file starts by reading the command line arguments and loading it's dependencies including properties and helper functions. It also create temp directories.
86
87h3. Instrumenting javascript code
88
89We need to create an instrumented copy of javascript code using JSCover. We pass in "no-instrument" args to tell jscover not instrument a file or directory and instead copy the original(s). This is good for ignoring vendor code, such as jquery which takes a long time to instrument and some vendor minified code breaks JSCover.
90
91h3. Exposing coverage data to EnvJasmine
92
93JSCover creates instrumented code that puts the output data into a local javascript variable. That variable is inaccessible to EnvJasmine. For now we're implementing a hack to find/replace the variable with an EnvJasmine scoped variable in the instrumented code files.
94
95h3. Run all EnvJasmine tests against the instrumented code
96
97Run the tests as normal against the instrumented code. Since the envjasmine-coverage plugin was included in @include/dependencies.js@ the plugin will write out the code coverage json data object to the reports directory.
98
99h3. Convert the json data to lcov format
100
101Sonar uses lcov format. JSCover has the ability to do this conversion.
102
103h3. Fix source code location in the lcov file
104
105For some reason, the source code root is set to the reports directory. Change this to the original source directory.
106
107h3. Remove branch data
108
109JSCover supports branch data. The Sonar javascript plugin doesn't. (It's on their roadmap.) For now we have to strip out branch data or sonar will break.
110
111h3. Run Sonar
112
113Runs sonar against the reports directory
114
115h3. Cleanup
116
117Deletes temp directories.
118
119h2. FAQ/Common Problems
120
121h3. Directory/Path problems
122
123It's really easy to be looking for the wrong file in the wrong place. The important directories are the source directory, the instrumented directory, the reports directory, and the lib directory. If you're having trouble make sure the files are all being read and output in the correct places. Debug statements can be helpful if you've lost your way setting this up.
124
125h3. Could not find jscoverage.json
126
127Exception in thread "main" java.lang.RuntimeException: Problem loading file: '/my/reports/directory/jscoverage.json'
128
129Make sure to uncomment the necessary lines in the dependencies.js files.
130
131h3. Everything looks like it's working correctly but Sonar is failing to include the coverage results.
132
133Check the final lcov file (@reports/jsTestDriver.conf-coverage.dat@). Each original javascript source file should be referenced by the correct absolute path. If the path is not correct, the "Fix source code location" step failed. Double check that your directories have trailing slashes EXCEPT for the reports directory which can't have a trailing slash.
134
135h3. What's with all the jsTestDriver references?
136
137The sonar javascript plugin has been designed to work with two different javascript testing frameworks. One of them is jsTestDriver. We have to select one in order for sonar to know that there's coverage data ready. But as long as the data output is the same, Sonar doesn't really care who made the output.
138
139h3. Why is the runner written in rhino/js? Wouldn't ruby/python/etc be easier to maintain?
140
141Yes it would. The prototypes were actually written in ruby and python, but I thought it would be more valuable to get rid of the external dependency.
142
143h2. Roadmap
144
145Add unit test success/failure metrics to sonar
146Add a .bat file
147Un-remove branch coverage (blocked by lack of sonar-javascript plugin support)
148Remove "data cleaning" when writing out coverage results (will require help jscover project modification)
149
150h2. Release Notes
151
152version beta: initial release. 1/11/2013
153version 1.7.1: Removed python dependency. Simplified configuration. Made sonar-runner the default sonar method. 1/17/2013
154version 1.7.2: Now works as ruby gem. Made many properties configurable from command line. There is no longer a default sonar method. 2/12/2013
155
156
157Please contact Daniel Freiman (https://github.com/DannyJF) or Trevor Lalish-Menagh (https://github.com/trevmex) through github with any defects or feature requests!
Note: See TracBrowser for help on using the repository browser.