mirror of
https://github.com/Smaug123/gitea-repo-config
synced 2025-10-05 15:38:41 +00:00
Rename to Gitea.Declarative (#4)
This commit is contained in:
29
Gitea.Declarative.Test/Gitea.Declarative.Test.fsproj
Normal file
29
Gitea.Declarative.Test/Gitea.Declarative.Test.fsproj
Normal file
@@ -0,0 +1,29 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Utils.fs" />
|
||||
<Compile Include="TestJsonSchema.fs" />
|
||||
<Compile Include="TestSwaggerJson.fs" />
|
||||
<Content Include="GiteaConfig.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FsUnit" Version="5.1.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
|
||||
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.14" />
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Gitea.Declarative.Lib\Gitea.Declarative.Lib.fsproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
34
Gitea.Declarative.Test/GiteaConfig.json
Normal file
34
Gitea.Declarative.Test/GiteaConfig.json
Normal file
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"users": {
|
||||
"admin": {
|
||||
"isAdmin": true,
|
||||
"email": "some-admin-email@example.com",
|
||||
"visibility": "private"
|
||||
},
|
||||
"nonadmin-user": {
|
||||
"isAdmin": false,
|
||||
"email": "some-nonadmin-email@example.com",
|
||||
"website": "https://example.com",
|
||||
"visibility": "public"
|
||||
}
|
||||
},
|
||||
"repos": {
|
||||
"nonadmin-user": {
|
||||
"synced-from-github-repo-1": {
|
||||
"description": "A repo that is imported from GitHub",
|
||||
"gitHub": "https://github.com/MyName/repo-name"
|
||||
},
|
||||
"synced-from-github-repo-2": {
|
||||
"description": "Another repo that is imported from GitHub",
|
||||
"gitHub": "https://github.com/MyName/repo-name-2"
|
||||
},
|
||||
"new-repo": {
|
||||
"description": "A repo that's created directly on this Gitea",
|
||||
"native": {
|
||||
"defaultBranch": "main",
|
||||
"private": false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
65
Gitea.Declarative.Test/TestJsonSchema.fs
Normal file
65
Gitea.Declarative.Test/TestJsonSchema.fs
Normal file
@@ -0,0 +1,65 @@
|
||||
namespace Gitea.Declarative.Test
|
||||
|
||||
open System.IO
|
||||
open System.Reflection
|
||||
open Gitea.Declarative
|
||||
open NUnit.Framework
|
||||
open FsUnitTyped
|
||||
open Newtonsoft.Json
|
||||
open Newtonsoft.Json.Schema
|
||||
open Newtonsoft.Json.Schema.Generation
|
||||
open Newtonsoft.Json.Serialization
|
||||
|
||||
[<TestFixture>]
|
||||
module TestSchema =
|
||||
let schemaGen = JSchemaGenerator ()
|
||||
schemaGen.ContractResolver <- CamelCasePropertyNamesContractResolver ()
|
||||
|
||||
[<Test>]
|
||||
let ``Schema is consistent`` () =
|
||||
let schemaFile =
|
||||
Assembly.GetExecutingAssembly().Location
|
||||
|> FileInfo
|
||||
|> fun fi -> fi.Directory
|
||||
|> Utils.findFileAbove "Gitea.Declarative.Lib/GiteaConfig.schema.json"
|
||||
|
||||
let existing = JSchema.Parse (File.ReadAllText schemaFile.FullName)
|
||||
let derived = schemaGen.Generate typeof<SerialisedGiteaConfig>
|
||||
|
||||
existing.ToString () |> shouldEqual (derived.ToString ())
|
||||
|
||||
[<Test>]
|
||||
let ``Example conforms to schema`` () =
|
||||
let executing = Assembly.GetExecutingAssembly().Location |> FileInfo
|
||||
let schemaFile = Utils.findFileAbove "GiteaConfig.json" executing.Directory
|
||||
|
||||
let existing = JSchema.Parse (File.ReadAllText schemaFile.FullName)
|
||||
|
||||
let jsonFile = Utils.findFileAbove "GiteaConfig.json" executing.Directory
|
||||
let json = File.ReadAllText jsonFile.FullName
|
||||
|
||||
use reader = new JsonTextReader (new StringReader (json))
|
||||
use validatingReader = new JSchemaValidatingReader (reader)
|
||||
validatingReader.Schema <- existing
|
||||
|
||||
let messages = ResizeArray ()
|
||||
validatingReader.ValidationEventHandler.Add (fun args -> messages.Add args.Message)
|
||||
|
||||
let ser = JsonSerializer ()
|
||||
ser.ContractResolver <- CamelCasePropertyNamesContractResolver ()
|
||||
let _config = ser.Deserialize<SerialisedGiteaConfig> validatingReader
|
||||
|
||||
messages |> shouldBeEmpty
|
||||
|
||||
[<Test>]
|
||||
[<Explicit "Run this to regenerate the schema file">]
|
||||
let ``Update schema file`` () =
|
||||
let schemaFile =
|
||||
Assembly.GetExecutingAssembly().Location
|
||||
|> FileInfo
|
||||
|> fun fi -> fi.Directory
|
||||
|> Utils.findFileAbove "Gitea.Declarative.Lib/GiteaConfig.schema.json"
|
||||
|
||||
let schema = schemaGen.Generate typeof<SerialisedGiteaConfig>
|
||||
|
||||
File.WriteAllText (schemaFile.FullName, schema.ToString ())
|
30
Gitea.Declarative.Test/TestSwaggerJson.fs
Normal file
30
Gitea.Declarative.Test/TestSwaggerJson.fs
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace Gitea.Declarative.Test
|
||||
|
||||
open System.IO
|
||||
open System.Net.Http
|
||||
open System.Reflection
|
||||
open NUnit.Framework
|
||||
|
||||
[<TestFixture>]
|
||||
module TestSwaggerJson =
|
||||
|
||||
[<Literal>]
|
||||
let GITEA_URL = "https://gitea.patrickstevens.co.uk"
|
||||
|
||||
[<Test>]
|
||||
[<Explicit>]
|
||||
let ``Update swagger file`` () : unit =
|
||||
let swaggerFile =
|
||||
Assembly.GetExecutingAssembly().Location
|
||||
|> FileInfo
|
||||
|> fun fi -> fi.Directory
|
||||
|> Utils.findFileAbove "Gitea/swagger.v1.json"
|
||||
|
||||
task {
|
||||
use client = new HttpClient ()
|
||||
let! stream = client.GetStreamAsync $"{GITEA_URL}/swagger.v1.json"
|
||||
use file = swaggerFile.OpenWrite ()
|
||||
do! stream.CopyToAsync file
|
||||
return ()
|
||||
}
|
||||
|> fun t -> t.Result
|
17
Gitea.Declarative.Test/Utils.fs
Normal file
17
Gitea.Declarative.Test/Utils.fs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Gitea.Declarative.Test
|
||||
|
||||
open System.IO
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module Utils =
|
||||
|
||||
let rec findFileAbove (fileName : string) (di : DirectoryInfo) =
|
||||
if isNull di then
|
||||
failwith "hit the root without finding anything"
|
||||
|
||||
let candidate = Path.Combine (di.FullName, fileName) |> FileInfo
|
||||
|
||||
if candidate.Exists then
|
||||
candidate
|
||||
else
|
||||
findFileAbove fileName di.Parent
|
Reference in New Issue
Block a user