This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8c77ee
commit 6652002
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace EasyLib.Files; | ||
|
||
/// <summary> | ||
/// Singleton to get and write the jobs to the state.json file. | ||
/// </summary> | ||
public class StateManager | ||
{ | ||
public readonly string StateFilePath; | ||
|
||
private StateManager() | ||
{ | ||
// AppData dir and append easysave/state.json | ||
var appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); | ||
var stateDirectory = Path.Combine(appDataDir, "easysave"); | ||
StateFilePath = Path.Combine(stateDirectory, "state.json"); | ||
|
||
// Create directory if it doesn't exist | ||
if (!Directory.Exists(stateDirectory)) | ||
{ | ||
Directory.CreateDirectory(stateDirectory); | ||
} | ||
|
||
// Create file and write [] if it doesn't exist | ||
if (!File.Exists(StateFilePath)) | ||
{ | ||
File.WriteAllText(StateFilePath, "[]"); | ||
} | ||
} | ||
|
||
public static StateManager Instance { get; } = new(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using EasyLib.Files; | ||
|
||
namespace EasySave.Tests.EasyLib.Files; | ||
|
||
public class StateManagerTests | ||
{ | ||
private string GetStateFilePath() | ||
{ | ||
var appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); | ||
var stateDirectory = Path.Combine(appDataDir, "easysave"); | ||
return Path.Combine(stateDirectory, "state.json"); | ||
} | ||
|
||
[Fact] | ||
public void Instance_ShouldBeSingleton() | ||
{ | ||
// Arrange | ||
|
||
// Act | ||
var instance1 = StateManager.Instance; | ||
var instance2 = StateManager.Instance; | ||
|
||
// Assert | ||
Assert.Same(instance1, instance2); | ||
} | ||
|
||
[Fact] | ||
public void StateFileCreation_ShouldCreateEmptyFile() | ||
{ | ||
// Arrange | ||
|
||
// Act | ||
var stateManager = StateManager.Instance; | ||
var stateFilePath = stateManager.StateFilePath; | ||
|
||
// Assert | ||
Assert.True(File.Exists(stateFilePath)); | ||
} | ||
|
||
[Fact] | ||
public void StateFileCreation_ShouldCreateJsonArray() | ||
{ | ||
// Arrange | ||
|
||
// Act | ||
var stateManager = StateManager.Instance; | ||
var stateFilePath = stateManager.StateFilePath; | ||
var fileContent = File.ReadAllText(stateFilePath).Trim(); | ||
|
||
// Assert | ||
Assert.StartsWith("[", fileContent); | ||
Assert.EndsWith("]", fileContent); | ||
} | ||
|
||
[Fact] | ||
public void StateFilePath_ShouldBeCorrect() | ||
{ | ||
// Arrange | ||
|
||
// Act | ||
var stateManager = StateManager.Instance; | ||
var stateFilePath = stateManager.StateFilePath; | ||
|
||
// Assert | ||
Assert.Equal(GetStateFilePath(), stateFilePath); | ||
} | ||
} |