Repository navigation
Keep the Seeded State When a Renamed File Has a Sidecar #1121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,104 @@ | ||
| using System.Text.Json.Nodes; | ||
| using AwesomeAssertions; | ||
| using PlexCleaner; | ||
| using ptr727.Utilities; | ||
| using Xunit; | ||
|
|
||
| namespace PlexCleanerTests; | ||
|
|
||
| [Collection("Sequential")] | ||
| public sealed class SidecarRenameStateTests : IDisposable | ||
| { | ||
| private const SidecarFile.StatesType Seeded = | ||
| SidecarFile.StatesType.ReMuxed | SidecarFile.StatesType.FileReNamed; | ||
|
|
||
| private readonly string _directory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
| private readonly ToolsOptions1 _toolsOptions = Program.Config.ToolsOptions; | ||
| private readonly MediaToolInfo _ffProbeInfo = Tools.FfProbe.Info; | ||
| private readonly MediaToolInfo _mkvMergeInfo = Tools.MkvMerge.Info; | ||
| private readonly MediaToolInfo _mediaInfoInfo = Tools.MediaInfo.Info; | ||
|
|
||
| public SidecarRenameStateTests() | ||
| { | ||
| _ = Directory.CreateDirectory(_directory); | ||
| Program.Config.ToolsOptions = new ToolsOptions1 | ||
| { | ||
| UseSystem = false, | ||
| RootRelative = false, | ||
| RootPath = _directory, | ||
| }; | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| Program.Config.ToolsOptions = _toolsOptions; | ||
| Tools.FfProbe.Info = _ffProbeInfo; | ||
| Tools.MkvMerge.Info = _mkvMergeInfo; | ||
| Tools.MediaInfo.Info = _mediaInfoInfo; | ||
| Directory.Delete(_directory, true); | ||
| } | ||
|
|
||
| private static int ReadState(string sidecarPath) => | ||
| JsonNode.Parse(File.ReadAllText(sidecarPath))!["State"]!.GetValue<int>(); | ||
|
|
||
| [Fact] | ||
| public void Open_SeededStateOverMatchingSidecar_KeepsBothStates() | ||
| { | ||
| // The sidecar name ignores the extension case, so the hash matches | ||
| string mediaFile = FakeTools.CopyUnverifiedSample(_directory); | ||
| string sidecarPath = Path.Combine(_directory, "name.PlexCleaner"); | ||
| JsonNode stored = JsonNode.Parse(File.ReadAllText(sidecarPath))!; | ||
| stored["State"] = (int)SidecarFile.StatesType.Verified; | ||
| File.WriteAllText(sidecarPath, stored.ToJsonString()); | ||
| string renamed = Path.Combine(_directory, "name.MKV"); | ||
| string tempName = Path.ChangeExtension(mediaFile, ".tmp7"); | ||
| File.Move(mediaFile, tempName); | ||
| File.Move(tempName, renamed); | ||
|
|
||
| SidecarFile sidecarFile = new(renamed) { State = Seeded }; | ||
| _ = sidecarFile.Open().Should().BeTrue(); | ||
|
|
||
| SidecarFile.StatesType expected = Seeded | SidecarFile.StatesType.Verified; | ||
| _ = sidecarFile.State.Should().Be(expected); | ||
| _ = ReadState(sidecarPath).Should().Be((int)expected); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Open_SeededStateOverStaleSidecar_KeepsSeededAndMarksModified() | ||
| { | ||
| Assert.SkipWhen(OperatingSystem.IsWindows(), "The fake tools are shell scripts"); | ||
| // A remuxed file meets the stale sidecar of the file it replaced | ||
| string mediaFile = FakeTools.CopyUnverifiedSample(_directory); | ||
| string sidecarPath = Path.Combine(_directory, "name.PlexCleaner"); | ||
| JsonNode stored = JsonNode.Parse(File.ReadAllText(sidecarPath))!; | ||
| stored["State"] = (int)SidecarFile.StatesType.Verified; | ||
| File.WriteAllText(sidecarPath, stored.ToJsonString()); | ||
| File.AppendAllText(mediaFile, "changed"); | ||
|
|
||
| // Tools return the stored sample data so the media update can run | ||
| foreach ( | ||
| (MediaTool tool, string key) in new (MediaTool, string)[] | ||
| { | ||
| (Tools.MediaInfo, "MediaInfoData"), | ||
| (Tools.MkvMerge, "MkvMergeInfoData"), | ||
| (Tools.FfProbe, "FfProbeInfoData"), | ||
| } | ||
| ) | ||
| { | ||
| string dataFile = Path.Combine(_directory, key + ".json"); | ||
| File.WriteAllText( | ||
| dataFile, | ||
| StringCompression.Decompress(stored[key]!.GetValue<string>()) | ||
| ); | ||
| FakeTools.WriteScript(tool.GetToolPath(), $"cat \"{dataFile}\"\n"); | ||
| } | ||
|
|
||
| SidecarFile sidecarFile = new(mediaFile) { State = Seeded }; | ||
| _ = sidecarFile.Open(true).Should().BeTrue(); | ||
|
|
||
| // The stale Verified flag is cleared, the seeded flags are kept | ||
| SidecarFile.StatesType expected = Seeded | SidecarFile.StatesType.FileModified; | ||
| _ = sidecarFile.State.Should().Be(expected); | ||
| _ = ReadState(sidecarPath).Should().Be((int)expected); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.