Skip to content

Commit

Permalink
Add BinaryContent module to AVPRIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
kMutagene committed Jun 19, 2024
1 parent d0183b6 commit bfaa3e7
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 115 deletions.
3 changes: 2 additions & 1 deletion src/AVPRIndex/AVPRIndex.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/RELEASE_NOTES.md"))</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageVersion>0.1.2</PackageVersion>
<PackageVersion>0.1.3</PackageVersion>
</PropertyGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\" />
<None Include="RELEASE_NOTES.md" />
<Compile Include="Globals.fs" />
<Compile Include="MD5Hash.fs" />
<Compile Include="BinaryContent.fs" />
<Compile Include="Utils.fs" />
<Compile Include="Domain.fs" />
<Compile Include="Frontmatter.fs" />
Expand Down
20 changes: 20 additions & 0 deletions src/AVPRIndex/BinaryContent.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace AVPRIndex

open System
open System.IO
open System.Text
open System.Security.Cryptography

type BinaryContent =

/// reads the content of the file at the given path and ensures that line endings are unified to `\n`
static member fromString (str: string) =
str.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes


/// reads the content of the file at the given path and ensures that line endings are unified to `\n`
static member fromFile (path: string) =
path
|> File.ReadAllText
|> BinaryContent.fromString
4 changes: 4 additions & 0 deletions src/AVPRIndex/RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.1.3

Add `BinaryContent` module to unify package content handling across downstream libraries

## v0.1.2

Add `PackageContentHash` module to unify package hash calculation across downstream libraries
Expand Down
63 changes: 63 additions & 0 deletions tests/IndexTests/BinaryContentTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
namespace BinaryContentTests

open System
open System.IO
open System.Text
open Xunit
open AVPRIndex
open AVPRIndex.Domain
open ReferenceObjects

module fromString =

[<Fact>]
let ``String with no line endings`` () =
let actual = BinaryContent.fromString BinaryContent.StringInput.noLineEndings
Assert.Equal<byte array>(BinaryContent.Content.noLineEndings, actual)

[<Fact>]
let ``String with CLRF`` () =
let actual = BinaryContent.fromString BinaryContent.StringInput.windowsLineEndings
Assert.Equal<byte array>(BinaryContent.Content.windowsLineEndings, actual)

[<Fact>]
let ``String with LF`` () =
let actual = BinaryContent.fromString BinaryContent.StringInput.unixLineEndings
Assert.Equal<byte array>(BinaryContent.Content.unixLineEndings, actual)

[<Fact>]
let ``String with mixed line endings`` () =
let actual = BinaryContent.fromString BinaryContent.StringInput.mixedLineEndings
Assert.Equal<byte array>(BinaryContent.Content.mixedLineEndings, actual)

module fromFile =

[<Fact>]
let ``mandatory binding frontmatter file`` () =
let actual = "fixtures/Frontmatter/Binding/[email protected]" |> BinaryContent.fromFile
Assert.Equal<byte array>(BinaryContent.Content.BindingFrontmatter.validMandatoryFrontmatter, actual)

[<Fact>]
let ``full binding frontmatter file`` () =
let actual = "fixtures/Frontmatter/Binding/[email protected]" |> BinaryContent.fromFile
Assert.Equal<byte array>(BinaryContent.Content.BindingFrontmatter.validFullFrontmatter, actual)

[<Fact>]
let ``invalid binding frontmatter file`` () =
let actual = "fixtures/Frontmatter/Binding/[email protected]" |> BinaryContent.fromFile
Assert.Equal<byte array>(BinaryContent.Content.BindingFrontmatter.invalidMissingMandatoryFrontmatter, actual)

[<Fact>]
let ``mandatory comment frontmatter file`` () =
let actual = "fixtures/Frontmatter/Comment/[email protected]" |> BinaryContent.fromFile
Assert.Equal<byte array>(BinaryContent.Content.CommentFrontmatter.validMandatoryFrontmatter, actual)

[<Fact>]
let ``full comment frontmatter file`` () =
let actual = "fixtures/Frontmatter/Comment/[email protected]" |> BinaryContent.fromFile
Assert.Equal<byte array>(BinaryContent.Content.CommentFrontmatter.validFullFrontmatter, actual)

[<Fact>]
let ``invalid comment frontmatter file`` () =
let actual = "fixtures/Frontmatter/Comment/[email protected]" |> BinaryContent.fromFile
Assert.Equal<byte array>(BinaryContent.Content.CommentFrontmatter.invalidMissingMandatoryFrontmatter, actual)
220 changes: 110 additions & 110 deletions tests/IndexTests/HashTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,120 +9,120 @@ open AVPRIndex.Domain
open ReferenceObjects


module hashString =
module hashString =

[<Fact>]
let ``String with no line endings`` () =
let actual = Hash.hashString Hash.Input.noLineEndings
Assert.Equal(Hash.Hashes.noLineEndings, actual)
[<Fact>]
let ``String with no line endings`` () =
let actual = Hash.hashString Hash.Input.noLineEndings
Assert.Equal(Hash.Hashes.noLineEndings, actual)

[<Fact>]
let ``String with CLRF`` () =
let actual = Hash.hashString Hash.Input.windowsLineEndings
Assert.Equal(Hash.Hashes.windowsLineEndings, actual)
[<Fact>]
let ``String with CLRF`` () =
let actual = Hash.hashString Hash.Input.windowsLineEndings
Assert.Equal(Hash.Hashes.windowsLineEndings, actual)

[<Fact>]
let ``String with LF`` () =
let actual = Hash.hashString Hash.Input.unixLineEndings
Assert.Equal(Hash.Hashes.unixLineEndings, actual)
[<Fact>]
let ``String with LF`` () =
let actual = Hash.hashString Hash.Input.unixLineEndings
Assert.Equal(Hash.Hashes.unixLineEndings, actual)

[<Fact>]
let ``String with mixed line endings`` () =
let actual = Hash.hashString Hash.Input.mixedLineEndings
Assert.Equal(Hash.Hashes.mixedLineEndings, actual)
[<Fact>]
let ``String with mixed line endings`` () =
let actual = Hash.hashString Hash.Input.mixedLineEndings
Assert.Equal(Hash.Hashes.mixedLineEndings, actual)

module hashContent =
module hashContent =

[<Fact>]
let ``mandatory binding frontmatter file`` () =
let actual =
"fixtures/Frontmatter/Binding/[email protected]"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> Hash.hashContent
Assert.Equal(Hash.Hashes.BindingFrontmatter.validMandatoryFrontmatter, actual)

[<Fact>]
let ``full binding frontmatter file`` () =
let actual =
"fixtures/Frontmatter/Binding/[email protected]"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> Hash.hashContent

Assert.Equal(Hash.Hashes.BindingFrontmatter.validFullFrontmatter, actual)

[<Fact>]
let ``invalid binding frontmatter file`` () =
let actual =
"fixtures/Frontmatter/Binding/[email protected]"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> Hash.hashContent
Assert.Equal(Hash.Hashes.BindingFrontmatter.invalidMissingMandatoryFrontmatter, actual)

[<Fact>]
let ``mandatory comment frontmatter file`` () =
let actual =
"fixtures/Frontmatter/Comment/[email protected]"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> Hash.hashContent

Assert.Equal(Hash.Hashes.CommentFrontmatter.validMandatoryFrontmatter, actual)

[<Fact>]
let ``full comment frontmatter file`` () =
let actual =
"fixtures/Frontmatter/Comment/[email protected]"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> Hash.hashContent
Assert.Equal(Hash.Hashes.CommentFrontmatter.validFullFrontmatter, actual)

[<Fact>]
let ``invalid comment frontmatter file`` () =
let actual =
"fixtures/Frontmatter/Comment/[email protected]"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> Hash.hashContent
Assert.Equal(Hash.Hashes.CommentFrontmatter.invalidMissingMandatoryFrontmatter, actual)

module hashFile =
[<Fact>]
let ``mandatory binding frontmatter file`` () =
let actual =
"fixtures/Frontmatter/Binding/[email protected]"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> Hash.hashContent
Assert.Equal(Hash.Hashes.BindingFrontmatter.validMandatoryFrontmatter, actual)

[<Fact>]
let ``full binding frontmatter file`` () =
let actual =
"fixtures/Frontmatter/Binding/[email protected]"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> Hash.hashContent

Assert.Equal(Hash.Hashes.BindingFrontmatter.validFullFrontmatter, actual)

[<Fact>]
let ``invalid binding frontmatter file`` () =
let actual =
"fixtures/Frontmatter/Binding/[email protected]"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> Hash.hashContent
Assert.Equal(Hash.Hashes.BindingFrontmatter.invalidMissingMandatoryFrontmatter, actual)

[<Fact>]
let ``mandatory comment frontmatter file`` () =
let actual =
"fixtures/Frontmatter/Comment/[email protected]"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> Hash.hashContent

Assert.Equal(Hash.Hashes.CommentFrontmatter.validMandatoryFrontmatter, actual)

[<Fact>]
let ``full comment frontmatter file`` () =
let actual =
"fixtures/Frontmatter/Comment/[email protected]"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> Hash.hashContent
Assert.Equal(Hash.Hashes.CommentFrontmatter.validFullFrontmatter, actual)

[<Fact>]
let ``invalid comment frontmatter file`` () =
let actual =
"fixtures/Frontmatter/Comment/[email protected]"
|> File.ReadAllText
|> fun s -> s.ReplaceLineEndings("\n")
|> Encoding.UTF8.GetBytes
|> Hash.hashContent
Assert.Equal(Hash.Hashes.CommentFrontmatter.invalidMissingMandatoryFrontmatter, actual)

module hashFile =

[<Fact>]
let ``mandatory binding frontmatter file`` () =
let actual = "fixtures/Frontmatter/Binding/[email protected]" |> Hash.hashFile
Assert.Equal(Hash.Hashes.BindingFrontmatter.validMandatoryFrontmatter, actual)

[<Fact>]
let ``full binding frontmatter file`` () =
let actual = "fixtures/Frontmatter/Binding/[email protected]" |> Hash.hashFile
Assert.Equal(Hash.Hashes.BindingFrontmatter.validFullFrontmatter, actual)

[<Fact>]
let ``invalid binding frontmatter file`` () =
let actual = "fixtures/Frontmatter/Binding/[email protected]" |> Hash.hashFile
Assert.Equal(Hash.Hashes.BindingFrontmatter.invalidMissingMandatoryFrontmatter, actual)

[<Fact>]
let ``mandatory comment frontmatter file`` () =
let actual = "fixtures/Frontmatter/Comment/[email protected]" |> Hash.hashFile
Assert.Equal(Hash.Hashes.CommentFrontmatter.validMandatoryFrontmatter, actual)

[<Fact>]
let ``full comment frontmatter file`` () =
let actual = "fixtures/Frontmatter/Comment/[email protected]" |> Hash.hashFile
Assert.Equal(Hash.Hashes.CommentFrontmatter.validFullFrontmatter, actual)

[<Fact>]
let ``invalid comment frontmatter file`` () =
let actual = "fixtures/Frontmatter/Comment/[email protected]" |> Hash.hashFile
Assert.Equal(Hash.Hashes.CommentFrontmatter.invalidMissingMandatoryFrontmatter, actual)
[<Fact>]
let ``mandatory binding frontmatter file`` () =
let actual = "fixtures/Frontmatter/Binding/[email protected]" |> Hash.hashFile
Assert.Equal(Hash.Hashes.BindingFrontmatter.validMandatoryFrontmatter, actual)

[<Fact>]
let ``full binding frontmatter file`` () =
let actual = "fixtures/Frontmatter/Binding/[email protected]" |> Hash.hashFile
Assert.Equal(Hash.Hashes.BindingFrontmatter.validFullFrontmatter, actual)

[<Fact>]
let ``invalid binding frontmatter file`` () =
let actual = "fixtures/Frontmatter/Binding/[email protected]" |> Hash.hashFile
Assert.Equal(Hash.Hashes.BindingFrontmatter.invalidMissingMandatoryFrontmatter, actual)

[<Fact>]
let ``mandatory comment frontmatter file`` () =
let actual = "fixtures/Frontmatter/Comment/[email protected]" |> Hash.hashFile
Assert.Equal(Hash.Hashes.CommentFrontmatter.validMandatoryFrontmatter, actual)

[<Fact>]
let ``full comment frontmatter file`` () =
let actual = "fixtures/Frontmatter/Comment/[email protected]" |> Hash.hashFile
Assert.Equal(Hash.Hashes.CommentFrontmatter.validFullFrontmatter, actual)

[<Fact>]
let ``invalid comment frontmatter file`` () =
let actual = "fixtures/Frontmatter/Comment/[email protected]" |> Hash.hashFile
Assert.Equal(Hash.Hashes.CommentFrontmatter.invalidMissingMandatoryFrontmatter, actual)
5 changes: 1 addition & 4 deletions tests/IndexTests/IndexTests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Compile Include="Utils.fs" />
<Compile Include="ReferenceObjects.fs" />
<Compile Include="HashTests.fs" />
<Compile Include="BinaryContentTests.fs" />
<Compile Include="DomainTests.fs" />
<Compile Include="FrontmatterTests.fs" />
<Compile Include="MetadataTests.fs" />
Expand All @@ -39,8 +40,4 @@
<ProjectReference Include="..\..\src\AVPRIndex\AVPRIndex.fsproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="fixtures\Hashes\" />
</ItemGroup>

</Project>
Loading

0 comments on commit bfaa3e7

Please sign in to comment.