|
| 1 | +using System; |
| 2 | +using GitHub.Extensions; |
| 3 | +using GitHub.Models; |
| 4 | +using GitHub.VisualStudio; |
| 5 | +using LibGit2Sharp; |
| 6 | +using NSubstitute; |
| 7 | +using UnitTests; |
| 8 | +using Xunit; |
| 9 | +using GitHub.Primitives; |
| 10 | +using System.Linq; |
| 11 | + |
| 12 | +public class SimpleRepositoryModelExtensionTests : TempFileBaseClass |
| 13 | +{ |
| 14 | + void SetupRepository(string sha) |
| 15 | + { |
| 16 | + var provider = Substitutes.ServiceProvider; |
| 17 | + Services.PackageServiceProvider = provider; |
| 18 | + var gitservice = provider.GetGitService(); |
| 19 | + var repo = Substitute.For<IRepository>(); |
| 20 | + gitservice.GetRepo(Args.String).Returns(repo); |
| 21 | + if (!String.IsNullOrEmpty(sha)) |
| 22 | + { |
| 23 | + var commit = Substitute.For<Commit>(); |
| 24 | + commit.Sha.Returns(sha); |
| 25 | + repo.Commits.Returns(new FakeCommitLog { commit }); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + [Theory] |
| 30 | + [InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")] |
| 31 | + [InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1")] |
| 32 | + [InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, 2, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1-L2")] |
| 33 | + [InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, 2, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")] |
| 34 | + [InlineData(false, "https://github.com/foo/bar", "123123", "", 1, 2, "https://github.com/foo/bar/commit/123123")] |
| 35 | + [InlineData(false, "https://github.com/foo/bar", "", @"src\dir\file1.cs", -1, 2, "https://github.com/foo/bar")] |
| 36 | + [InlineData(false, "https://github.com/foo/bar", null, null, -1, -1, "https://github.com/foo/bar")] |
| 37 | + [InlineData(false, null, "123123", @"src\dir\file1.cs", 1, 2, null)] |
| 38 | + [InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")] |
| 39 | + [InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1")] |
| 40 | + [InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, 2, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1-L2")] |
| 41 | + [InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, 2, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")] |
| 42 | + [InlineData(true, "https://github.com/foo/bar", "", @"src\dir\file1.cs", -1, 2, "https://github.com/foo/bar")] |
| 43 | + [InlineData(true, null, "123123", @"src\dir\file1.cs", 1, 2, null)] |
| 44 | + public void GenerateUrl(bool createRootedPath, string baseUrl, string sha, string path, int startLine, int endLine, string expected) |
| 45 | + { |
| 46 | + SetupRepository(sha); |
| 47 | + |
| 48 | + var basePath = Directory.CreateSubdirectory("generate-url-test1"); |
| 49 | + if (createRootedPath && path != null) |
| 50 | + path = System.IO.Path.Combine(basePath.FullName, path); |
| 51 | + ISimpleRepositoryModel model = null; |
| 52 | + if (!String.IsNullOrEmpty(baseUrl)) |
| 53 | + model = new SimpleRepositoryModel("bar", new UriString(baseUrl), basePath.FullName); |
| 54 | + else |
| 55 | + model = new SimpleRepositoryModel(basePath.FullName); |
| 56 | + var result = model.GenerateUrl(path, startLine, endLine); |
| 57 | + Assert.Equal(expected, result?.ToString()); |
| 58 | + } |
| 59 | +} |
0 commit comments