Rename to Gitea.Declarative (#4)

This commit is contained in:
Patrick Stevens
2022-12-30 10:26:59 +00:00
committed by GitHub
parent c409daf659
commit 4ae932072e
25 changed files with 133 additions and 129 deletions

View 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>

View 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
}
}
}
}
}

View 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 ())

View 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

View 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