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
196 changes: 196 additions & 0 deletions .github/workflows/persian-errors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
name: Build PersianErrors (SE5)

on:
push:
branches: [main]
paths:
- 'se5/PersianErrors/**'
- 'se5/PersianErrors.Tests/**'
- 'se5/Plugin-Shared/**'
- '.github/workflows/persian-errors.yml'
pull_request:
paths:
- 'se5/PersianErrors/**'
- 'se5/PersianErrors.Tests/**'
- 'se5/Plugin-Shared/**'
- '.github/workflows/persian-errors.yml'
workflow_dispatch:
inputs:
release:
description: 'Publish a GitHub release. Bumps the minor in plugin.json and commits it back.'
type: boolean
default: false
version:
description: 'Optional explicit version (e.g. 1.2.0). Overrides the auto-minor-bump.'
required: false
type: string

permissions:
contents: write

jobs:
prepare:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.set.outputs.version }}
tag: ${{ steps.set.outputs.tag }}
ref: ${{ steps.set.outputs.ref }}
release: ${{ steps.set.outputs.release }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Resolve version (and bump on release)
id: set
run: |
set -e
current=$(jq -r .version se5/PersianErrors/plugin.json)
release_flag="false"
ref="${{ github.sha }}"
version="$current"
tag=""

if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ inputs.release }}" = "true" ]; then
release_flag="true"
if [ -n "${{ inputs.version }}" ]; then
version="${{ inputs.version }}"
else
IFS=. read -r major minor patch <<< "$current"
version="$major.$((minor + 1)).0"
fi

tmp=$(mktemp)
jq --arg v "$version" '.version = $v' se5/PersianErrors/plugin.json > "$tmp"
mv "$tmp" se5/PersianErrors/plugin.json

git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add se5/PersianErrors/plugin.json
git commit -m "Bump PersianErrors to $version [skip ci]"

# Retry-with-rebase: if a concurrent release dispatched for a different
# plugin won the push first, pull --rebase and try again.
attempts=0
while ! git push 2>/tmp/push.err; do
attempts=$((attempts + 1))
if [ "$attempts" -ge 5 ]; then
echo "Failed to push after $attempts attempts:" >&2
cat /tmp/push.err >&2
exit 1
fi
echo "Push rejected (attempt $attempts), pulling --rebase and retrying..."
git pull --rebase --no-edit
done
ref=$(git rev-parse HEAD)

IFS=. read -r major minor patch <<< "$version"
tag="se5-persian-errors-v$major.$minor"
fi

echo "version=$version" >> "$GITHUB_OUTPUT"
echo "tag=$tag" >> "$GITHUB_OUTPUT"
echo "ref=$ref" >> "$GITHUB_OUTPUT"
echo "release=$release_flag" >> "$GITHUB_OUTPUT"

test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup .NET 8
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'

- name: Test
run: dotnet test se5/PersianErrors.Tests/PersianErrors.Tests.csproj -c Release

build:
needs: [prepare, test]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- rid: win-x64
os: windows
exe: PersianErrors.exe
- rid: win-arm64
os: windows
exe: PersianErrors.exe
- rid: linux-x64
os: linux
exe: PersianErrors
- rid: linux-arm64
os: linux
exe: PersianErrors
- rid: osx-x64
os: macos
exe: PersianErrors
- rid: osx-arm64
os: macos
exe: PersianErrors

steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ needs.prepare.outputs.ref }}

- name: Setup .NET 8
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'

- name: Publish self-contained (${{ matrix.rid }})
working-directory: se5/PersianErrors
run: |
dotnet publish PersianErrors.csproj \
-c Release \
-r ${{ matrix.rid }} \
--self-contained true \
-p:DebugType=None \
-p:DebugSymbols=false \
-o staging/PersianErrors

- name: Rewrite plugin.json for ${{ matrix.os }}
working-directory: se5/PersianErrors
run: |
jq '
del(.runtime, .entry) |
.executables = { "${{ matrix.os }}": "${{ matrix.exe }}" }
' plugin.json > staging/PersianErrors/plugin.json

- name: Package zip
working-directory: se5/PersianErrors/staging
run: zip -r "$GITHUB_WORKSPACE/PersianErrors-${{ matrix.rid }}.zip" PersianErrors

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: PersianErrors-${{ matrix.rid }}
path: PersianErrors-${{ matrix.rid }}.zip

release:
name: Publish GitHub Release
needs: [prepare, build]
if: needs.prepare.outputs.release == 'true'
runs-on: ubuntu-latest
steps:
- name: Download all zips
uses: actions/download-artifact@v4
with:
path: dist
pattern: PersianErrors-*
merge-multiple: true

- name: Create release and upload zips
env:
GH_TOKEN: ${{ github.token }}
run: |
gh release create "${{ needs.prepare.outputs.tag }}" dist/PersianErrors-*.zip \
--repo "${{ github.repository }}" \
--target "${{ needs.prepare.outputs.ref }}" \
--title "PersianErrors v${{ needs.prepare.outputs.version }}" \
--notes "Self-contained PersianErrors plugin builds for win/linux/osx (x64 + arm64)."
112 changes: 112 additions & 0 deletions se5/PersianErrors.Tests/MainViewModelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using SubtitleEdit.Plugins.PersianErrors;
using SubtitleEdit.Plugins.Shared;

namespace PersianErrors.Tests;

public class MainViewModelTests
{
private static async Task<MainViewModel> CreateAsync(PluginRequest request)
{
var vm = new MainViewModel(request);
await WaitAsync(vm);
return vm;
}

private static async Task WaitAsync(MainViewModel vm)
{
for (var i = 0; i < 300 && vm.IsBusy; i++)
{
await Task.Delay(10);
}

Assert.False(vm.IsBusy);
}

private static PluginRequest Request(params string[] texts) => new()
{
Subtitle = new PluginSubtitle
{
Format = "Advanced Sub Station Alpha",
Paragraphs = texts.Select((t, i) => new PluginParagraph { StartMs = i * 1000, EndMs = i * 1000 + 900, Text = t, Style = "Persian", Actor = "A" }).ToList(),
},
};

[Fact]
public async Task OkReturnsTheLinesInTheirOwnFormat()
{
var vm = await CreateAsync(Request("شدني", "درست"));

var response = vm.BuildResponse();

Assert.Equal(PluginStatus.Ok, response.Status);
Assert.Equal("Advanced Sub Station Alpha", response.Subtitle!.Format);
Assert.Equal(new[] { "شدنی", "درست" }, response.Subtitle.Paragraphs!.Select(p => p.Text));
Assert.All(response.Subtitle.Paragraphs!, p => Assert.Equal("Persian", p.Style));
}

[Fact]
public async Task UnselectedFixesAreLeftOut()
{
var vm = await CreateAsync(Request("شدني", "املاك"));
vm.VisibleFixes.Single(f => f.Index == 0).IsSelected = false;

var response = vm.BuildResponse();

Assert.Equal(new[] { "شدني", "املاک" }, response.Subtitle!.Paragraphs!.Select(p => p.Text));
}

[Fact]
public async Task NoFixesIsCancelled()
{
var vm = await CreateAsync(Request("درست"));

Assert.Equal(PluginStatus.Cancelled, vm.BuildResponse().Status);
}

[Fact]
public async Task OlderSubtitleEditGetsSubRipBack()
{
var request = new PluginRequest { Subtitle = new PluginSubtitle { SubRip = "1\n00:00:01,000 --> 00:00:02,000\nشدني\n" } };
var vm = await CreateAsync(request);

var response = vm.BuildResponse();

Assert.Equal("SubRip", response.Subtitle!.Format);
Assert.Contains("شدنی", response.Subtitle.Native);
Assert.Null(response.Subtitle.Paragraphs);
}

[Fact]
public async Task SettingsRoundTrip()
{
var first = await CreateAsync(Request("شدني"));
first.Groups.Single(g => g.Name == "OCR").IsEnabled = false;
first.ShowHidden = false;
var saved = first.BuildCancelResponse();

var request = Request("شدني");
request.Settings = saved.Settings;
request.SettingsVersion = saved.SettingsVersion;
var second = await CreateAsync(request);

Assert.False(second.Groups.Single(g => g.Name == "OCR").IsEnabled);
Assert.False(second.ShowHidden);
Assert.Equal(second.Groups.Count - 1, second.EnabledGroupCount);
}

[Fact]
public async Task ApplyAndRecheckThenUndo()
{
var vm = await CreateAsync(Request("شدني"));

vm.ApplyAndRecheckCommand.Execute(null);
await WaitAsync(vm);
Assert.Equal(1, vm.ChangedLineCount);
Assert.Equal(0, vm.FixCount);

vm.UndoCommand.Execute(null);
await WaitAsync(vm);
Assert.Equal(0, vm.ChangedLineCount);
Assert.Equal(1, vm.FixCount);
}
}
27 changes: 27 additions & 0 deletions se5/PersianErrors.Tests/PersianErrors.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<PropertyGroup>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PersianErrors\PersianErrors.csproj" />
</ItemGroup>

</Project>
Loading
Loading