33import com .clouds42 .CommandLineOptions .ConnectionOptions ;
44import com .clouds42 .CommandLineOptions .MetadataOptions ;
55import com .clouds42 .CommandLineOptions .OutputOptions ;
6- import com .github ._1c_syntax .bsl .parser .BSLLexer ;
7- import com .github ._1c_syntax .bsl .parser .BSLParser ;
8- import com .github ._1c_syntax .bsl .parser .BSLParserRuleContext ;
9- import com .github ._1c_syntax .bsl .parser .BSLTokenizer ;
10- import com .github ._1c_syntax .bsl .parser .Tokenizer ;
11- import com .github ._1c_syntax .mdclasses .mdo .MDObjectBase ;
12- import com .github ._1c_syntax .mdclasses .mdo .SettingsStorage ;
13- import com .github ._1c_syntax .mdclasses .metadata .Configuration ;
14- import com .github ._1c_syntax .mdclasses .metadata .additional .MDOModule ;
15- import com .github ._1c_syntax .mdclasses .metadata .additional .ModuleType ;
16- import com .github ._1c_syntax .mdclasses .metadata .additional .SupportVariant ;
6+ import com .github ._1c_syntax .bsl .parser .*;
7+ import com .github ._1c_syntax .mdclasses .mdo .AbstractMDObjectBase ;
8+ import com .github ._1c_syntax .mdclasses .mdo .MDSettingsStorage ;
9+ import com .github ._1c_syntax .mdclasses .Configuration ;
10+ import com .github ._1c_syntax .mdclasses .mdo .support .MDOModule ;
11+ import com .github ._1c_syntax .mdclasses .mdo .support .ModuleType ;
12+ import com .github ._1c_syntax .mdclasses .supportconf .SupportVariant ;
1713import de .vandermeer .asciitable .AsciiTable ;
1814import de .vandermeer .asciitable .CWC_LongestLine ;
1915import org .antlr .v4 .runtime .Token ;
4642import java .nio .file .Files ;
4743import java .nio .file .Path ;
4844import java .nio .file .Paths ;
45+ import java .time .Instant ;
4946import java .util .*;
5047import java .util .regex .Matcher ;
5148import java .util .regex .Pattern ;
@@ -56,13 +53,13 @@ public class Utils {
5653
5754 private static final Logger logger = LoggerFactory .getLogger (MethodHandles .lookup ().lookupClass ());
5855
59- public static String getModuleTypeUuid (ModuleType moduleType , MDObjectBase mdObject ) {
56+ public static String getModuleTypeUuid (ModuleType moduleType , AbstractMDObjectBase mdObject ) {
6057 if (moduleType == ModuleType .CommandModule ) {
6158 return "078a6af8-d22c-4248-9c33-7e90075a3d2c" ;
6259 } else if (moduleType == ModuleType .ObjectModule ) {
6360 return "a637f77f-3840-441d-a1c3-699c8c5cb7e0" ;
6461 } else if (moduleType == ModuleType .ManagerModule ) {
65- if (mdObject instanceof SettingsStorage ) {
62+ if (mdObject instanceof MDSettingsStorage ) {
6663 return "0c8cad23-bf8c-468e-b49e-12f1927c048b" ;
6764 } else {
6865 return "d1b64a2c-8078-4982-8190-8f81aefda192" ;
@@ -92,7 +89,7 @@ public static String getModuleTypeUuid(ModuleType moduleType, MDObjectBase mdObj
9289 return "UNKNOWN" ;
9390 }
9491
95- private static String getUriKey (String mdObjUuid , ModuleType moduleType , MDObjectBase mdObj ) {
92+ private static String getUriKey (String mdObjUuid , ModuleType moduleType , AbstractMDObjectBase mdObj ) {
9693 return mdObjUuid + "/" + getModuleTypeUuid (moduleType , mdObj );
9794 }
9895
@@ -135,7 +132,8 @@ private static void addCoverageData(Map<URI, Map<BigDecimal, Integer>> coverageD
135132
136133 if (linesToCover .length > 0 ) {
137134
138- List <Token > comments = tokenizer .getTokens ().stream ()
135+ List <Token > allTokens = tokenizer .getTokens ();
136+ List <Token > comments = allTokens .stream ()
139137 .filter (token -> token .getType () == BSLLexer .LINE_COMMENT )
140138 .collect (Collectors .toList ());
141139
@@ -220,7 +218,7 @@ public static Map<String, URI> readMetadata(MetadataOptions metadataOptions,
220218 Configuration conf = Configuration .create (rootPath );
221219
222220 for (MDOModule module : conf .getModules ()) {
223- MDObjectBase mdObj = module .getOwner ();
221+ AbstractMDObjectBase mdObj = module .getOwner ();
224222
225223 String mdObjUuid = mdObj .getUuid ();
226224
@@ -347,11 +345,123 @@ public static void dumpCoverageFile(Map<URI, Map<BigDecimal, Integer>> coverageD
347345 dumpGenericCoverageFile (coverageData , metadataOptions , outputOptions );
348346 } else if (outputOptions .getOutputFormat () == OutputOptions .OutputFormat .LCOV ) {
349347 dumpLcovFile (coverageData , metadataOptions , outputOptions );
348+ } else if (outputOptions .getOutputFormat () == OutputOptions .OutputFormat .COBERTURA ) {
349+ dumpCoberturaFile (coverageData , metadataOptions , outputOptions );
350350 } else {
351351 logger .info ("Unknown format" );
352352 }
353353 }
354354
355+ private static void dumpCoberturaFile (Map <URI , Map <BigDecimal , Integer >> coverageData ,
356+ MetadataOptions metadataOptions ,
357+ OutputOptions outputOptions ) {
358+ DocumentBuilderFactory icFactory = DocumentBuilderFactory .newInstance ();
359+ DocumentBuilder icBuilder ;
360+ try {
361+ long linesToCover = 0 ;
362+ long coveredLinesCount = 0 ;
363+ for (Map <BigDecimal , Integer > bigDecimalMap : coverageData .values ()) {
364+ linesToCover += bigDecimalMap .values ().stream ().filter (value -> value >= 0 ).count ();
365+ coveredLinesCount += bigDecimalMap .values ().stream ().filter (value -> value > 0 ).count ();
366+ }
367+ logger .info ("Lines to cover: " + linesToCover );
368+ logger .info ("Covered lines: " + coveredLinesCount );
369+ if (linesToCover > 0 ) {
370+ logger .info ("Coverage: " + Math .floorDiv (coveredLinesCount * 10000 , linesToCover ) / 100. + "%" );
371+ }
372+
373+ URI projectUri = Path .of (metadataOptions .getProjectDirName ()).toUri ();
374+ icBuilder = icFactory .newDocumentBuilder ();
375+ Document doc = icBuilder .newDocument ();
376+ Element mainRootElement = doc .createElement ("coverage" );
377+ String lineRate = "0.0" ;
378+ if (linesToCover > 0 ) {
379+ lineRate = String .valueOf (Math .floorDiv (coveredLinesCount * 100 , linesToCover ) / 100. );
380+ }
381+ mainRootElement .setAttribute ("line-rate" , lineRate );
382+ mainRootElement .setAttribute ("branch-rate" , "0.0" );
383+ mainRootElement .setAttribute ("lines-covered" , String .valueOf (coveredLinesCount ));
384+ mainRootElement .setAttribute ("lines-valid" , String .valueOf (linesToCover ));
385+ mainRootElement .setAttribute ("branches-covered" , "0" );
386+ mainRootElement .setAttribute ("branches-valid" , "0" );
387+ mainRootElement .setAttribute ("complexity" , "0" );
388+ mainRootElement .setAttribute ("version" , "0" );
389+ mainRootElement .setAttribute ("timestamp" , String .valueOf (Instant .now ().getEpochSecond ()));
390+ doc .appendChild (mainRootElement );
391+
392+ Element sourcesElement = doc .createElement ("sources" );
393+ mainRootElement .appendChild (sourcesElement );
394+
395+ Element sourceElement = doc .createElement ("source" );
396+ String [] projectDirArray = projectUri .getPath ().split ("/" );
397+ if (projectDirArray .length > 2 ) {
398+ sourceElement .setTextContent ("/builds/" + projectDirArray [projectDirArray .length - 2 ] + "/" + projectDirArray [projectDirArray .length - 1 ] + "/" );
399+ } else {
400+ sourcesElement .setTextContent (projectUri .getPath ());
401+ }
402+ sourcesElement .appendChild (sourceElement );
403+
404+ Element packagesElement = doc .createElement ("packages" );
405+ mainRootElement .appendChild (packagesElement );
406+
407+ Element packageElement = doc .createElement ("package" );
408+ packageElement .setAttribute ("name" , "Main" );
409+ packageElement .setAttribute ("line-rate" , lineRate );
410+ packageElement .setAttribute ("branch-rate" , "0.0" );
411+ packageElement .setAttribute ("complexity" , "0" );
412+ packagesElement .appendChild (packageElement );
413+
414+ Element classesElement = doc .createElement ("classes" );
415+ packageElement .appendChild (classesElement );
416+
417+ coverageData .forEach ((uri , bigDecimalsMap ) -> {
418+ if (bigDecimalsMap .isEmpty ()) {
419+ return ;
420+ }
421+
422+ long fileLinesToCover = bigDecimalsMap .values ().stream ().filter (value -> value >= 0 ).count ();
423+ long fileCoveredLinesCount = bigDecimalsMap .values ().stream ().filter (value -> value > 0 ).count ();
424+ String fileLineRate = "0.0" ;
425+ if (fileLinesToCover > 0 ) {
426+ fileLineRate = String .valueOf (Math .floorDiv (fileCoveredLinesCount * 100 , fileLinesToCover ) / 100. );
427+ }
428+
429+ Element classElement = doc .createElement ("class" );
430+ classElement .setAttribute ("name" , projectUri .relativize (uri ).getPath ());
431+ classElement .setAttribute ("line-rate" , fileLineRate );
432+ classElement .setAttribute ("branch-rate" , "0.0" );
433+ classElement .setAttribute ("complexity" , "0" );
434+ classElement .setAttribute ("filename" , projectUri .relativize (uri ).getPath ());
435+ classesElement .appendChild (classElement );
436+
437+ Element methodsElement = doc .createElement ("methods" );
438+ classElement .appendChild (methodsElement );
439+
440+ bigDecimalsMap .forEach ((bigDecimal , hits ) -> {
441+ if (hits >= 0 ) {
442+ Element lineElement = doc .createElement ("line" );
443+ lineElement .setAttribute ("hits" , String .valueOf (hits ));
444+ lineElement .setAttribute ("number" , bigDecimal .toString ());
445+ lineElement .setAttribute ("branch" , "false" );
446+ classElement .appendChild (lineElement );
447+ }
448+ });
449+ });
450+ Transformer transformer = TransformerFactory .newInstance ().newTransformer ();
451+ transformer .setOutputProperty (OutputKeys .INDENT , "yes" );
452+ DOMSource source = new DOMSource (doc );
453+ StreamResult outputStream ;
454+ if (outputOptions .getOutputFile () == null ) {
455+ outputStream = new StreamResult (System .out );
456+ } else {
457+ outputStream = new StreamResult (new FileOutputStream (outputOptions .getOutputFile ()));
458+ }
459+ transformer .transform (source , outputStream );
460+ } catch (Exception e ) {
461+ logger .error (e .getLocalizedMessage ());
462+ }
463+ }
464+
355465 private static void dumpGenericCoverageFile (Map <URI , Map <BigDecimal , Integer >> coverageData ,
356466 MetadataOptions metadataOptions ,
357467 OutputOptions outputOptions ) {
@@ -537,7 +647,9 @@ public static String getPipeName(ConnectionOptions connectionOptions) throws IOE
537647 pipeName = String .format ("\\ \\ .\\ pipe\\ COVER_%s_%s" , connectionOptions .getInfobaseAlias (),
538648 debugUri .toString ().replaceAll ("[^a-zA-Z0-9-_.]" , "_" ));
539649 } else {
540- Path tempDir = Files .createTempDirectory ("coverage41c" );
650+ File tempDirFile = new File ("/tmp/coverage41c/" );
651+ tempDirFile .mkdirs ();
652+ Path tempDir = tempDirFile .toPath ();
541653 Path sock = tempDir .resolve (String .format ("%s_%s.sock" , connectionOptions .getInfobaseAlias (),
542654 debugUri .toString ().replaceAll ("[^a-zA-Z0-9-_.]" , "_" )));
543655 pipeName = sock .toString ();
0 commit comments