-
Notifications
You must be signed in to change notification settings - Fork 11
Added extensions methods #2
Changes from 2 commits
25d0a38
403fd75
5f94116
6edb3dc
22437c2
9de105f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace GitTools.Tests | ||
{ | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class StringExtensionsFacts | ||
{ | ||
[TestCase("/develop", false)] | ||
[TestCase("/master", false)] | ||
[TestCase("/pr/25", true)] | ||
[TestCase("/pull/25", true)] | ||
[TestCase("/pull-requests/25", true)] | ||
public void TheIsPullRequestMethod(string input, bool expectedValue) | ||
{ | ||
var actualValue = input.IsPullRequest(); | ||
|
||
Assert.AreEqual(expectedValue, actualValue); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,30 @@ | ||
namespace GitTools | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
// TODO: Constants go here | ||
using System; | ||
using LibGit2Sharp; | ||
|
||
public static class TestValues | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering if we can make this class name better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to come up with something better, as long as it's not Constants ;-) |
||
{ | ||
private static DateTimeOffset _simulatedTime = DateTimeOffset.Now.AddHours(-1); | ||
|
||
public static DateTimeOffset Now | ||
{ | ||
get | ||
{ | ||
_simulatedTime = _simulatedTime.AddMinutes(1); | ||
return _simulatedTime; | ||
} | ||
} | ||
|
||
public static Signature SignatureNow() | ||
{ | ||
var dateTimeOffset = Now; | ||
return Signature(dateTimeOffset); | ||
} | ||
|
||
public static Signature Signature(DateTimeOffset dateTimeOffset) | ||
{ | ||
return new Signature("A. U. Thor", "[email protected]", dateTimeOffset); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace GitTools | ||
{ | ||
using System.Text; | ||
|
||
public static class StringBuilderExtensions | ||
{ | ||
//[StringFormatMethod("format")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should add the r# annotations nuget to make the usage of these light up? |
||
public static void AppendLineFormat(this StringBuilder stringBuilder, string format, params object[] args) | ||
{ | ||
stringBuilder.AppendFormat(format, args); | ||
stringBuilder.AppendLine(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
namespace GitTools | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
|
||
public static class ProcessHelper | ||
{ | ||
private static volatile object _lockObject = new object(); | ||
|
||
// http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/f6069441-4ab1-4299-ad6a-b8bb9ed36be3 | ||
public static Process Start(ProcessStartInfo startInfo) | ||
{ | ||
Process process; | ||
|
||
lock (_lockObject) | ||
{ | ||
using (new ChangeErrorMode(ErrorModes.FailCriticalErrors | ErrorModes.NoGpFaultErrorBox)) | ||
{ | ||
process = Process.Start(startInfo); | ||
process.PriorityClass = ProcessPriorityClass.Idle; | ||
} | ||
} | ||
|
||
return process; | ||
} | ||
|
||
// http://csharptest.net/532/using-processstart-to-capture-console-output/ | ||
public static int Run(Action<string> output, Action<string> errorOutput, TextReader input, string exe, string args, string workingDirectory, params KeyValuePair<string, string>[] environmentalVariables) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should be using threads to read this. I should add an issue to fix this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably. I just copy/pasted this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, not for this PR anyways. Just a note to myself really |
||
{ | ||
if (String.IsNullOrEmpty(exe)) | ||
{ | ||
throw new FileNotFoundException(); | ||
} | ||
|
||
if (output == null) | ||
{ | ||
throw new ArgumentNullException("output"); | ||
} | ||
|
||
var psi = new ProcessStartInfo | ||
{ | ||
UseShellExecute = false, | ||
RedirectStandardError = true, | ||
RedirectStandardOutput = true, | ||
RedirectStandardInput = true, | ||
WindowStyle = ProcessWindowStyle.Hidden, | ||
CreateNoWindow = true, | ||
ErrorDialog = false, | ||
WorkingDirectory = workingDirectory ?? Environment.CurrentDirectory, | ||
FileName = exe, | ||
Arguments = args | ||
}; | ||
|
||
foreach (var environmentalVariable in environmentalVariables) | ||
{ | ||
if (!psi.EnvironmentVariables.ContainsKey(environmentalVariable.Key) && environmentalVariable.Value != null) | ||
psi.EnvironmentVariables.Add(environmentalVariable.Key, environmentalVariable.Value); | ||
if (psi.EnvironmentVariables.ContainsKey(environmentalVariable.Key) && environmentalVariable.Value == null) | ||
psi.EnvironmentVariables.Remove(environmentalVariable.Key); | ||
} | ||
|
||
using (var process = Process.Start(psi)) | ||
{ | ||
using (var mreOut = new ManualResetEvent(false)) | ||
{ | ||
using (var mreErr = new ManualResetEvent(false)) | ||
{ | ||
process.EnableRaisingEvents = true; | ||
process.OutputDataReceived += (o, e) => | ||
{ | ||
// ReSharper disable once AccessToDisposedClosure | ||
if (e.Data == null) | ||
{ | ||
mreOut.Set(); | ||
} | ||
else | ||
{ | ||
output(e.Data); | ||
} | ||
}; | ||
process.BeginOutputReadLine(); | ||
process.ErrorDataReceived += (o, e) => | ||
{ | ||
// ReSharper disable once AccessToDisposedClosure | ||
if (e.Data == null) | ||
{ | ||
mreErr.Set(); | ||
} | ||
else | ||
{ | ||
errorOutput(e.Data); | ||
} | ||
}; | ||
|
||
process.BeginErrorReadLine(); | ||
|
||
string line; | ||
while (input != null && null != (line = input.ReadLine())) | ||
{ | ||
process.StandardInput.WriteLine(line); | ||
} | ||
|
||
process.StandardInput.Close(); | ||
process.WaitForExit(); | ||
|
||
mreOut.WaitOne(); | ||
mreErr.WaitOne(); | ||
|
||
return process.ExitCode; | ||
} | ||
} | ||
} | ||
} | ||
|
||
[Flags] | ||
public enum ErrorModes | ||
{ | ||
Default = 0x0, | ||
FailCriticalErrors = 0x1, | ||
NoGpFaultErrorBox = 0x2, | ||
NoAlignmentFaultExcept = 0x4, | ||
NoOpenFileErrorBox = 0x8000 | ||
} | ||
|
||
public struct ChangeErrorMode : IDisposable | ||
{ | ||
private readonly int _oldMode; | ||
|
||
public ChangeErrorMode(ErrorModes mode) | ||
{ | ||
_oldMode = SetErrorMode((int)mode); | ||
} | ||
|
||
void IDisposable.Dispose() { SetErrorMode(_oldMode); } | ||
|
||
[DllImport("kernel32.dll")] | ||
static extern int SetErrorMode(int newMode); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm quite a fan of Shouldly for assertions.
input.IsPullRequest().ShouldBe(expectedValue)
will give quite good error messages.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give you this one. It's all a matter of preference, to be honest I think nunit provides really good feedback (I expected x, but got y). Not sure how shouldly does it but I'll do it anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldly actually grabs the source code from your code to use in the assertion. It should give better error messages in all cases :)