Skip to content

Commit cc1a61a

Browse files
Merge pull request #54 from nzbart/process-exit-code
Set process exit code to 0 on success, 1 on failure
2 parents c3205eb + 8cab5a0 commit cc1a61a

9 files changed

Lines changed: 64 additions & 20 deletions

src/ScmBackup.Tests/ErrorHandlingScmBackupTests.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ namespace ScmBackup.Tests
66
{
77
public class ErrorHandlingScmBackupTests
88
{
9-
[Fact]
10-
public void LogsWhenExceptionIsThrown()
9+
static (FakeLogger FakeLogger, ErrorHandlingScmBackup ErrorHandlingScmBackup) BuildFakeScmBackup()
1110
{
1211
var ex = new Exception("!!!");
1312
var subBackup = new FakeScmBackup();
@@ -21,12 +20,30 @@ public void LogsWhenExceptionIsThrown()
2120
var logger = new FakeLogger();
2221

2322
var backup = new ErrorHandlingScmBackup(subBackup, logger, context);
23+
return (logger, backup);
24+
}
25+
26+
[Fact]
27+
public void LogsWhenExceptionIsThrown()
28+
{
29+
var (logger, backup) = BuildFakeScmBackup();
30+
2431
backup.Run();
2532

2633
Assert.True(logger.LoggedSomething);
2734
Assert.Equal(ErrorLevel.Error, logger.LastErrorLevel);
2835
// we can't check whether the last exception is the exception from above,
2936
// because there are more logging outputs after the exception.
3037
}
38+
39+
[Fact]
40+
public void ReturnsFalseWhenExceptionIsThrown()
41+
{
42+
var (_, backup) = BuildFakeScmBackup();
43+
44+
var result = backup.Run();
45+
46+
Assert.False(result);
47+
}
3148
}
3249
}

src/ScmBackup.Tests/FakeScmBackup.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ namespace ScmBackup.Tests
55
public class FakeScmBackup : IScmBackup
66
{
77
public Exception ToThrow { get; set; }
8+
public bool ToReturn { get; set; } = true;
89

9-
public void Run()
10+
public bool Run()
1011
{
1112
if (this.ToThrow != null)
1213
{
1314
throw this.ToThrow;
1415
}
16+
17+
return ToReturn;
1518
}
1619
}
1720
}

src/ScmBackup.Tests/LogMailingScmBackupTests.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace ScmBackup.Tests
55
{
66
public class LogMailingScmBackupTests
77
{
8-
[Fact]
9-
public void RunSendsMail()
8+
static (FakeEmailSender mail, LogMailingScmBackup sut) BuildFakeLogMailingScmBackup(bool innerReturnValue)
109
{
1110
var subBackup = new FakeScmBackup();
11+
subBackup.ToReturn = innerReturnValue;
1212

1313
var messages = new LogMessages();
1414
messages.AddMessage("1");
@@ -17,12 +17,32 @@ public void RunSendsMail()
1717
var mail = new FakeEmailSender();
1818

1919
var sut = new LogMailingScmBackup(subBackup, messages, mail);
20+
return (mail, sut);
21+
}
22+
23+
[Fact]
24+
public void RunSendsMail()
25+
{
26+
var (mail, sut) = BuildFakeLogMailingScmBackup(true);
27+
2028
sut.Run();
2129

2230
Assert.NotNull(mail.LastSubject);
2331
Assert.NotNull(mail.LastBody);
2432
Assert.Contains("1", mail.LastBody);
2533
Assert.Contains("2", mail.LastBody);
2634
}
35+
36+
[Theory]
37+
[InlineData(true)]
38+
[InlineData(false)]
39+
public void RunReturnsValueFromInnerExecution(bool innerReturnValue)
40+
{
41+
var (mail, sut) = BuildFakeLogMailingScmBackup(innerReturnValue);
42+
43+
var result = sut.Run();
44+
45+
Assert.Equal(result, innerReturnValue);
46+
}
2747
}
2848
}

src/ScmBackup/ErrorHandlingScmBackup.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,19 @@ public ErrorHandlingScmBackup(IScmBackup backup, ILogger logger, IContext contex
2727
this.WaitSecondsOnError = 5;
2828
}
2929

30-
public void Run()
30+
public bool Run()
3131
{
32-
bool ok = false;
3332
string className = this.GetType().Name;
3433

3534
try
3635
{
3736
this.logger.Log(ErrorLevel.Debug, Resource.StartingBackup, className);
38-
this.backup.Run();
39-
ok = true;
37+
return this.backup.Run();
4038
}
4139
catch (Exception ex)
4240
{
4341
this.logger.Log(ErrorLevel.Error, ex.Message);
44-
}
4542

46-
if (!ok)
47-
{
4843
// Wait as many seconds as defined in the config.
4944
// If we don't have the config value because the exception was thrown while reading the config, use the default value defined in this class
5045
int seconds = this.WaitSecondsOnError;
@@ -57,6 +52,8 @@ public void Run()
5752
this.logger.Log(ErrorLevel.Error, Resource.BackupFailed);
5853
this.logger.Log(ErrorLevel.Error, Resource.EndSeconds, seconds);
5954
Task.Delay(TimeSpan.FromSeconds(seconds)).Wait();
55+
56+
return false;
6057
}
6158
}
6259
}

src/ScmBackup/IScmBackup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
internal interface IScmBackup
44
{
5-
void Run();
5+
bool Run();
66
}
77
}

src/ScmBackup/LogMailingScmBackup.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ public LogMailingScmBackup(IScmBackup backup, ILogMessages messages, IEmailSende
2121
this.mail = mail;
2222
}
2323

24-
public void Run()
24+
public bool Run()
2525
{
26-
this.backup.Run();
26+
var result = this.backup.Run();
2727

2828
string subject = string.Format(Resource.LogMailSubject, DateTime.Now.ToString("dd MMM HH:mm:ss"));
2929
string body = string.Join(Environment.NewLine, this.messages.GetMessages().ToArray());
3030

3131
this.mail.Send(subject, body);
32+
33+
return result;
3234
}
3335
}
3436
}

src/ScmBackup/LoggingScmBackup.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@ public LoggingScmBackup(IScmBackup backup, IContext context, ILogger logger)
1313
this.logger = logger;
1414
}
1515

16-
public void Run()
16+
public bool Run()
1717
{
1818
logger.Log(ErrorLevel.Info, this.context.AppTitle);
1919
logger.Log(ErrorLevel.Info, Resource.AppWebsite);
2020

2121
// TODO: log more stuff (operating system, configuration...)
2222

23-
this.backup.Run();
23+
var result = this.backup.Run();
2424

2525
logger.Log(ErrorLevel.Info, Resource.BackupFinished);
2626
logger.Log(ErrorLevel.Info, string.Format(Resource.BackupFinishedDirectory, this.context.Config.LocalFolder));
27+
28+
return result;
2729
}
2830
}
2931
}

src/ScmBackup/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ namespace ScmBackup
44
{
55
public class Program
66
{
7-
public static void Main(string[] args)
7+
public static int Main(string[] args)
88
{
99
var container = Bootstrapper.BuildContainer();
10-
container.GetInstance<IScmBackup>().Run();
10+
var success = container.GetInstance<IScmBackup>().Run();
11+
return success ? 0 : 1;
1112
}
1213
}
1314
}

src/ScmBackup/ScmBackup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ScmBackup(IApiCaller apiCaller, IScmValidator validator, IBackupMaker bac
2121
this.configBackupMaker = configBackupMaker;
2222
}
2323

24-
public void Run()
24+
public bool Run()
2525
{
2626
this.configBackupMaker.BackupConfigs();
2727

@@ -36,6 +36,8 @@ public void Run()
3636
{
3737
this.backupMaker.Backup(source, repos.GetReposForSource(source));
3838
}
39+
40+
return true;
3941
}
4042
}
4143
}

0 commit comments

Comments
 (0)