Skip to content

Fix for: Cannot read property 'classes' of undefined #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/coberturaReportParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ function processReport(xml, computation) {
xml.packages.package.forEach(function(packageObject) {
processPackage(packageObject, computation);
});

} else {
} else if (xml.packages.package) {
processPackage(xml.packages.package, computation);
} else {
processPackage(xml.packages, computation);
}
}

Expand All @@ -31,15 +32,20 @@ function processReport(xml, computation) {
* @param {Object} computation is the result of the computation.
*/
function processPackage(packageObject, computation) {
if (packageObject.classes.class instanceof Array) {
if (packageObject.classes && packageObject.classes.class instanceof Array) {
// Process each individual class
packageObject.classes.class.forEach(function(clazz) {
processClass(clazz, computation);
});

} else {
} else if (packageObject.classes && packageObject.classes.class) {
// Single class to be processed
processClass(packageObject.classes.class, computation);
} else if (packageObject.class && packageObject.class instanceof Array) {
packageObject.class.forEach(function(clazz) {
processClass(clazz, computation);
});
} else {
processClass(packageObject.class, computation);
}
}

Expand Down
48 changes: 48 additions & 0 deletions test/fixture/istanbul-report-multiple-packages-without-classes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" ?>
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
<coverage lines-valid="328" lines-covered="255" line-rate="0.7774" branches-valid="58" branches-covered="30" branch-rate="0.5172" timestamp="1444266082605" complexity="0" version="0.1">
<sources>
<source>/home/mdesales/dev/github-intuit/servicesplatform-nodejs/sp-quality</source>
</sources>
<packages>
<class name="lint.js" filename="lint.js" line-rate="1" branch-rate="1" >
<methods>
</methods>
<lines>
<line number="5" hits="1" branch="false" />
<line number="10" hits="1" branch="false" />
</lines>
</class>
<class name="test.js" filename="test.js" line-rate="0.6" branch-rate="1" >
<methods>
<method name="(anonymous_1)" hits="0" signature="()V" >
<lines><line number="8" hits="0" /></lines>
</method>
<method name="(anonymous_2)" hits="8" signature="()V" >
<lines><line number="15" hits="8" /></lines>
</method>
<method name="(anonymous_3)" hits="0" signature="()V" >
<lines><line number="22" hits="0" /></lines>
</method>
<method name="(anonymous_4)" hits="0" signature="()V" >
<lines><line number="29" hits="0" /></lines>
</method>
<method name="(anonymous_5)" hits="0" signature="()V" >
<lines><line number="37" hits="0" /></lines>
</method>
</methods>
<lines>
<line number="8" hits="1" branch="false" />
<line number="9" hits="0" branch="false" />
<line number="15" hits="1" branch="false" />
<line number="16" hits="8" branch="false" />
<line number="22" hits="1" branch="false" />
<line number="23" hits="0" branch="false" />
<line number="29" hits="1" branch="false" />
<line number="30" hits="0" branch="false" />
<line number="37" hits="1" branch="false" />
<line number="38" hits="0" branch="false" />
</lines>
</class>
</packages>
</coverage>
41 changes: 41 additions & 0 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,47 @@ describe("istanbul-cobertura-badger", function() {
});
});

it("should compute overall coverage over packages without classes and create the badge", function(done) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

// Use the fixture that's without problems
// Using defaults directory /coverage/
var opts = {
badgeFileName: "coverage-multiple-without-classes",
destinationDir: destinationDir,
istanbulReportFile: path.resolve(__dirname, "fixture", "istanbul-report-multiple-packages-without-classes.xml")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.
'__dirname' is not defined.

};

// Load the badge for the report
badger(opts, function parsingResults(err, badgeStatus) {
expect(err).to.be.null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected an assignment or function call and instead saw an expression.

expect(badgeStatus).to.be.an("object");

console.log(badgeStatus);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'console' is not defined.


var fileWithExtension = opts.badgeFileName + ".svg";
var coverageBadgePath = path.normalize(path.resolve(destinationDir, fileWithExtension));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

expect(coverageBadgePath.indexOf(fileWithExtension)).to.be.above(0);

// Verify that the badge file was successfully created
fs.stat(coverageBadgePath, function gettingStats(err, stats) {
expect(err).to.be.null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected an assignment or function call and instead saw an expression.

expect(stats).to.be.an("object");
expect(stats.isFile()).to.be.true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected an assignment or function call and instead saw an expression.

expect(stats.size).to.be.above(0);

fs.unlink(coverageBadgePath, function(err) {
if (err) {
console.log(err);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'console' is not defined.

}
console.log("Deleted the badge file " + coverageBadgePath);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'console' is not defined.


done();
});

});

});
});

});

describe("Parsing reports with xml problems", function() {
Expand Down