1+ using System . Collections . Generic ;
2+ using System . Threading . Tasks ;
3+ using Moq ;
4+ using NUnit . Framework ;
5+ using Octokit ;
6+ using ReleaseCleaner . Invocation ;
7+
8+ namespace ReleaseCleaner . Filtering
9+ {
10+ [ TestFixture ]
11+ [ Category ( "Core Logic" ) ]
12+ [ Category ( "Filtering" ) ]
13+ public class OrphanMatcherTests
14+ {
15+ private readonly Mock < IGitHubClient > clientMock = new Mock < IGitHubClient > ( ) ;
16+ private OrphanMatcher cut ;
17+
18+ [ SetUp ]
19+ public void TestSetup ( )
20+ {
21+ clientMock . Reset ( ) ;
22+ cut = new OrphanMatcher ( clientMock . Object ) ;
23+ }
24+
25+ [ Test ]
26+ public void NoOrphanFlag_DoesNotIncurRequest ( )
27+ {
28+ var args = ArgumentsMockData . BaseBuilder ( ) ;
29+ // OrphanOnly defaults to false
30+ cut . Prepare ( args . Build ( ) ) ;
31+ var result = cut . Matches ( null ) ;
32+ Assert . IsTrue ( result ) ;
33+ clientMock . VerifyNoOtherCalls ( ) ;
34+ }
35+
36+ [ Test ]
37+ public void PrepareWithOrphanFlag_HitsApi ( )
38+ {
39+ var args = ArgumentsMockData . BaseBuilder ( ) ;
40+ args . Orphans ( ) ;
41+
42+ var repositoryMock = new Mock < IRepositoriesClient > ( ) ;
43+ clientMock . SetupGet ( client => client . Repository ) . Returns ( repositoryMock . Object ) ;
44+ repositoryMock . Setup ( repository => repository . GetAllTags ( ArgumentsMockData . Owner , ArgumentsMockData . Project ) )
45+ . Returns ( Task . FromResult < IReadOnlyList < RepositoryTag > > ( new List < RepositoryTag > ( ) ) )
46+ . Verifiable ( ) ;
47+ cut . Prepare ( args . Build ( ) ) ;
48+
49+ Mock . VerifyAll ( clientMock , repositoryMock ) ;
50+ clientMock . VerifyNoOtherCalls ( ) ;
51+ repositoryMock . VerifyNoOtherCalls ( ) ;
52+ }
53+
54+ [ TestCase ( true , "exists" , false , Description = "Draft and existing Tag is not orphan" ) ]
55+ [ TestCase ( true , "missing" , true , Description = "Draft and missing Tag is an orphan" ) ]
56+ [ TestCase ( false , "exists" , false , Description = "NonDraft and existing Tag is not orphan" ) ]
57+ [ TestCase ( false , "missing" , false , Description = "NonDraft and missing Tag is not orphan" ) ]
58+ public void OrphaningLogic ( bool draftStatus , string tagName , bool expected )
59+ {
60+ var release = new Release (
61+ null , null , null , null , 0 , null ,
62+ tagName : tagName ,
63+ null , null , null ,
64+ draft : draftStatus , false , default , null , default , null , null , new List < ReleaseAsset > ( )
65+ ) ;
66+
67+ var args = ArgumentsMockData . BaseBuilder ( ) ;
68+ args . Orphans ( ) ;
69+
70+ var repositoryMock = new Mock < IRepositoriesClient > ( ) ;
71+ clientMock . SetupGet ( client => client . Repository ) . Returns ( repositoryMock . Object ) ;
72+ repositoryMock . Setup ( repository => repository . GetAllTags ( ArgumentsMockData . Owner , ArgumentsMockData . Project ) )
73+ . Returns ( Task . FromResult < IReadOnlyList < RepositoryTag > > ( new List < RepositoryTag > ( new [ ] { new RepositoryTag ( "exists" , default , default , default , default ) } ) ) )
74+ . Verifiable ( ) ;
75+ cut . Prepare ( args . Build ( ) ) ;
76+
77+ Assert . AreEqual ( expected , cut . Matches ( release ) ) ;
78+
79+ Mock . VerifyAll ( clientMock , repositoryMock ) ;
80+ clientMock . VerifyNoOtherCalls ( ) ;
81+ repositoryMock . VerifyNoOtherCalls ( ) ;
82+ }
83+ }
84+ }
0 commit comments