0

I am using Git on Windows and trying to include unit testing in the workflow. If the commit message contain a keyword. The commit-msg hook will trigger Powershell command to run some Nunit tests.

This is my ruby code in the hook

#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)

puts "The commit message is " + message

$regex = /(#runtest)/

if $regex.match(message)
    exec 'powershell.exe -ExecutionPolicy RemoteSigned -Command {RunNunitTestCase}'
end

However when I commit a changes, the result is like below. The exec line was run but do nothing.

PS D:\testfolder> git commit -am '#runtest'
The commit message is #runtest
[authorupdate b14878d] 123
 1 files changed, 1 insertions(+), 0 deletions(-)

I am new to ruby and powershell. Feel free to comment if this workflow is feasible or you have a better approach.

Thank you.

2 Answers 2

2

The curly bracket needs to be escaped.

exec "powershell.exe -ExecutionPolicy RemoteSigned -Command & \{RunNunitTestCase\}
Sign up to request clarification or add additional context in comments.

Comments

1

I tried the exact script with the exec line being:

exec 'powershell.exe -ExecutionPolicy RemoteSigned -Command gps'

and it did print out the output of gps when I did git commit -m "#runtest"

So it does run the command and it does work.

Make sure that whatever you are executing does work. Grab the powershell line and try it out on commandline directly.

2 Comments

Thank you @manojlds. I found the problem on the curly bracket. It need to be escaped. exec "powershell.exe -ExecutionPolicy RemoteSigned -Command & \{RunNunitTestCase\}" Or I can simply remove the brackets in this case.
@seanbun - If it helped you, you can accept this as answer ( the tick to the left)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.