zzzuperfly
zzzuperfly

Reputation: 401

capture cmd output

we are moving from ccnet to teamcity so my question might be just ignorant but anyway:

I have a cmd line hack that checks the build for "Todo" comments and output the count:

findstr /s /i "TODO" *.cs | find /c "":""

or

findstr /s /i TODO *.cs | find /c "":"" >todo.txt

now I want to fail the build if it is larger than 0 but I have trouble figuring out how to do that. I believe it would be neatest if I could inject the result into the buildlog

how do I do this?

Upvotes: 0

Views: 1324

Answers (2)

zzzuperfly
zzzuperfly

Reputation: 401

it turns out it wasn´t my cmdline that was the problem but the way I had called it from teamcity. Still your input dbenham is very useful since I could fail the build with your suggestion and still show the number of offending lines.

Much obliged :)

the correct usage shown below!

picture of buildstep in teamcity with the command line properly setup

Upvotes: 0

dbenham
dbenham

Reputation: 130819

If I understand your requirements correctly, you are looking to "fail the build" if you find at least one line within any .cs file that contains the string TODO as well as ? (some other string).

I believe your FIND command is broken. As written it looks for an empty string within a file named :. I believe you want to search the output of the piped FINDSTR command. But I don't understand what string you want to look for with FIND.

The FIND search string must be enclosed in double quotes - "search". If the search is to include a double quote, then each double quote must be escaped with another double quote. To search for She said "Hello", you would need "She said ""Hello""".

I suspect you used the FIND command because you want the count. But since you are only looking for the presence of strings (count>0), you don't need the actual count. The output can be redirected to nul and the && operator can be used to take action if the string was found.

You should be able to perform your search with a single FINDSTR command using a regular expression. I'm going to assume you are looking for any line that contains : followed at some point by TODO (case insensitive). The expression .* matches any character other than newline 0 or more times. So your search string would be :.*TODO. If I got the : wrong, and you need to include a double quote in the search, then each double quote needs to be escaped with a backslash like \".

You are doing a recursive case insensitive regular expression search within all sub-directories, so you want the /s, /r and /i options.

FINDSTR will split any string into multiple searches at each space unless you use the /c: option. I don't think your search has a space, but I like to use the /c option any way. Typically the search string is enclosed in double quotes as well.

The complete command would be

>nul findstr /sri /c:":.*TODO" *.cs && echo Do something to make the build fail

My problem is I don't know TeamCity, so I don't know how to signal TeamCity to abort the build. I just printed out a message if the search string is found, but obviously some other action needs to be taken.

EDIT in response to comments

OK - based on your comment it looks like TeamCity escapes double quote with another double quote. So the command that is actually executed in your original question becomes

findstr /s /i TODO *.cs | find /c ":" >todo.txt

which is, as you say, perfectly fine.

In that case, my suggestion of using a single FINDSTR should be perfect. Since your search does not require spaces, we can eliminate the quotes all together. If TeamCity can examine the returning error code and take action, then it is really simple.

>nul findstr /sri :.*TODO *.cs

The error code will be 0 if the string was found, 1 if it was not found.

If you need the error code to be 1 if found and 0 if not found, then simply use

>nul findstr /sri :.*TOTO *.cs && (exit 1) || (exit 0)


If you really want to use your original command

findstr /s /i "TODO" *.cs | >nul find /c "":""

will return error code 0 if found, 1 if not found

findstr /s /i "TODO" *.cs | >nul find /c "":"" && (exit 1) || (exit 0)

will return error code 1 if found, 0 if not found

Upvotes: 1

Related Questions