Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"Hooks.Run": "Running hook: {0}",
"Hooks.Run.WaitingForEnd": "Waiting for the end...",
"Hooks.Run.Error": "Running hook error: {0}",
"Hooks.Run.ErrorCode": "Hook exited with error code: {0}",

"Update.CheckingAvailableUpdate": "Checking for updates...",
"Update.FailedCheck": "Failed to check for updates. Check your Internet connection.",
Expand Down
1 change: 1 addition & 0 deletions lang/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"Hooks.Run": "Uruchamianie skryptu: {0}",
"Hooks.Run.WaitingForEnd": "Oczekiwanie na zakończenie...",
"Hooks.Run.Error": "Błąd uruchamiania skryptu: {0}",
"Hooks.Run.ErrorCode": "Skrypt zakończył się kodem błędu: {0}",

"Update.CheckingAvailableUpdate": "Sprawdzanie dostępności aktualizacji...",
"Update.FailedCheck": "Nie udało się sprawdzić dostępności aktualizacji. Sprawdź połączenie internetowe.",
Expand Down
1 change: 1 addition & 0 deletions lang/sk.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"Hooks.Run": "Vykonávam hook: {0}",
"Hooks.Run.WaitingForEnd": "Čakám na dokončenie...",
"Hooks.Run.Error": "Chyba pri vykonávaní hooku: {0}",
"Hooks.Run.ErrorCode": "Hook exited with error code: {0}",

"Update.CheckingAvailableUpdate": "Kontrola aktualizácii...",
"Update.FailedCheck": "Nepodarilo sa skontrolovať aktualizácie. Skontrolujte internetové pripojenie.",
Expand Down
42 changes: 39 additions & 3 deletions src/gmbt/src/Hooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,47 @@ public void Run()

try
{
var process = Process.Start(Command);

var processStartInfo = new ProcessStartInfo(Command);
var process = new Process();
process.StartInfo = processStartInfo;

Logger.Normal("Hooks.Run.WaitingForEnd".Translate());

process.WaitForExit();
if (Logger.Verbosity >= VerbosityLevel.Detailed)
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.OutputDataReceived += (object sender, DataReceivedEventArgs args) =>
{
if (args.Data != null)
{
Logger.Detailed(args.Data);
}
};
process.ErrorDataReceived += (object sender, DataReceivedEventArgs args) =>
{
if (args.Data != null)
{
Logger.Error(args.Data);
}
};
}

process.Start();

if (Logger.Verbosity >= VerbosityLevel.Detailed)
{
process.BeginOutputReadLine();
process.BeginErrorReadLine();
}

process.WaitForExit();

if (process.ExitCode != 0)
{
Logger.Warn("Hooks.Run.ErrorCode".Translate(process.ExitCode));
}
}
catch (Exception ex)
{
Expand Down
1 change: 1 addition & 0 deletions tests/gmbt/SampleFiles/exit_error.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit -1
5 changes: 5 additions & 0 deletions tests/gmbt/SampleFiles/hooks.fail.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
hooks:
test:
post:
assetsMerge:
- "exit_error.bat"
5 changes: 5 additions & 0 deletions tests/gmbt/SampleFiles/hooks.stdout.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
hooks:
test:
post:
assetsMerge:
- "stdout.bat"
4 changes: 4 additions & 0 deletions tests/gmbt/SampleFiles/stdout.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@echo off
echo "Message to stdout #1"
echo "Message to stdout #2"
echo "Message to stderr #1" 1>&2
12 changes: 12 additions & 0 deletions tests/gmbt/gmbt.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="SampleFiles\stdout.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SampleFiles\exit_error.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SampleFiles\hooks.stdout.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SampleFiles\hooks.fail.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SampleFiles\hooks.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
52 changes: 52 additions & 0 deletions tests/gmbt/src/HooksTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace GMBT.Tests
{
Expand Down Expand Up @@ -38,5 +39,56 @@ public void Hooks_Test()
Command = "d"
});
}

[TestMethod]
[DeploymentItem("SampleFiles\\exit_error.bat")]
[DeploymentItem("SampleFiles\\hooks.fail.yml")]
public void Hooks_FatalOnHookError()
{
Internationalization.Init("en");
Logger.Verbosity = VerbosityLevel.Minimal;

var config = ConfigDeserializer.Deserialize("hooks.fail.yml");

var manager = new HooksManager();

manager.RegisterHooks(config.Hooks);

var stringWriter = new StringWriter();
System.Console.SetOut(stringWriter);

manager.RunHooks(HookMode.Test, HookType.Post, HookEvent.AssetsMerge);

var output = stringWriter.ToString();

Assert.IsTrue(output.Contains("Hook exited with error code: -1"));
}


[TestMethod]
[DeploymentItem("SampleFiles\\stdout.bat")]
[DeploymentItem("SampleFiles\\hooks.stdout.yml")]
public void Hooks_StandardOutput()
{
Internationalization.Init("en");
Logger.Verbosity = VerbosityLevel.Detailed;

var config = ConfigDeserializer.Deserialize("hooks.stdout.yml");

var manager = new HooksManager();

manager.RegisterHooks(config.Hooks);

var stringWriter = new StringWriter();
System.Console.SetOut(stringWriter);

manager.RunHooks(HookMode.Test, HookType.Post, HookEvent.AssetsMerge);

var output = stringWriter.ToString();

Assert.IsTrue(output.Contains("Message to stdout #1"));
Assert.IsTrue(output.Contains("Message to stdout #2"));
Assert.IsTrue(output.Contains("Message to stderr #1"));
}
}
}