Better way to fix mocha lcov output using sed

376 Views Asked by At

Due to the know prob of mocha-lcov-mocha breaking file paths, I need to fix the current output paths that looks like this:

SF:Vis/test-Guid.coffee
SF:Vis/Guid.coffee
SF:Vis/test-Vis-Edge.coffee
SF:Vis/Vis-Edge.coffee

into

SF:test/Vis/test-Guid.coffee
SF:src/Vis/Guid.coffee
SF:test/Vis/test-Vis-Edge.coffee
SF:src/Vis/Vis-Edge.coffee

I'm not very good with sed, but I got it to work using:

mocha -R mocha-lcov-reporter _coverage/test --recursive | sed 's,SF:,SF:src/,' | sed s',SF.*test.*,SF:test//&,' | sed s',/SF:,,' | sed s',test/src,test,' | ./node_modules/coveralls/bin/coveralls.js

which is basically doing 4 sed commands in sequence

sed 's,SF:,SF:src/,' 
sed s',SF.*test.*,SF:test//&,'
sed s',/SF:,,' 
sed s',test/src,test,' 

my question is if there is a way to do with this one sed command, or use another osx/linux command line tool

4

There are 4 best solutions below

1
On BEST ANSWER

Initially put "src/" after every ":" and then if "test" is found on the line replace "src" with "test":

$ sed 's,:,:src/,;/test/s,src,test,' file
SF:test/Vis/test-Guid.coffee
SF:src/Vis/Guid.coffee
SF:test/Vis/test-Vis-Edge.coffee
SF:src/Vis/Vis-Edge.coffee
0
On

sed command

 sed '\#test#!{s#SF:Vis/#SF:src/Vis/#g};\#SF:Vis/test#{s#SF:Vis/test#SF:test/Vis/test#g};' my_file
0
On

You could put all the sed commands in a file, one line per command, and just use "sed -e script". But if you just want it on a single command-line, separate with semicolons. This works for me:

sed 's,SF:,SF:src/,;s,SF.*test.*,SF:test//&,;s,SF:,,;s,test/src/,test,'
0
On

Here is an awk version:

awk -F: '/SF/ {$0=$1FS (/test/?"test/":"src/")$2}1' file
SF:test/Vis/test-Guid.coffee
SF:src/Vis/Guid.coffee
SF:test/Vis/test-Vis-Edge.coffee
SF:src/Vis/Vis-Edge.coffee

How it works:

awk -F: '                               # Set field separator to ":"
/SF/{                                   # Does line start with "SF"?
    $0=$1FS (/test/?"test/":"src/")$2   # Recreat String by adding "test" if line contains "test", else "src"
    }
1                                       # Print all lines
' file                                  # read the file