Verified Commit c4717fe5 authored by Loïc Dachary's avatar Loïc Dachary
Browse files

forges: file: add fixtures functions instead of static files

parent 17cc5d1e
Pipeline #991 failed with stage
in 1 minute and 30 seconds
......@@ -17,56 +17,35 @@ package file
import (
"bytes"
"os/exec"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func assertFileEquals(t *testing.T, a, b *File, filename string) {
fa := filepath.Join(a.options.Directory, filename)
fb := filepath.Join(b.options.Directory, filename)
cmd := exec.Command("diff", "-r", fa, fb)
func assertFileEquals(t *testing.T, a, b *File) {
cmd := exec.Command("diff", "-r", a.options.Directory, b.options.Directory)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
assert.NoError(t, cmd.Run(), out.String())
}
func originalAndCopy(t *testing.T) (original, copy *File) {
tmpDir := t.TempDir()
copy = &File{}
copy.Init(Options{
Directory: tmpDir,
Validation: true,
})
func TestFile(t *testing.T) {
fixture := NewFixture(t)
fixture.CreateProject()
original = &File{}
original.Init(Options{Directory: "testdata/repo1"})
return
}
func TestFileProject(t *testing.T) {
original, copy := originalAndCopy(t)
copy.CreateProject(original.GetProject())
assert.EqualValues(t, original.GetProject(), copy.GetProject())
assertFileEquals(t, copy, original, "project.json")
}
func TestFileIssues(t *testing.T) {
original, copy := originalAndCopy(t)
original, copy := fixture.OriginalAndCopy()
assert.NotEmpty(t, original.GetIssues())
copy.CreateIssues(original.GetIssues()...)
assert.EqualValues(t, original.GetIssues(), copy.GetIssues())
assertFileEquals(t, copy, original, "issue.json")
}
func TestFileReleases(t *testing.T) {
original, copy := originalAndCopy(t)
assert.NotEmpty(t, original.GetReleases())
copy.CreateReleases(original.GetReleases()...)
assert.EqualValues(t, original.GetReleases(), copy.GetReleases())
assertFileEquals(t, copy, original, "release.json")
copy.CreateProject(original.GetProject())
assert.EqualValues(t, original.GetProject(), copy.GetProject())
assertFileEquals(t, copy, original)
}
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package file
import (
"testing"
"time"
"lab.forgefriends.org/friendlyforgeformat/gofff/format"
)
type Fixture struct {
t *testing.T
d string
f *File
}
func NewFixture(t *testing.T) *Fixture {
directory := t.TempDir()
f := &File{}
f.Init(Options{Directory: directory})
return &Fixture{
t: t,
d: directory,
f: f,
}
}
func (f *Fixture) OriginalAndCopy() (original, copy *File) {
copy = &File{}
copy.Init(Options{
Directory: f.t.TempDir(),
Validation: true,
})
original = f.f
return
}
func (f *Fixture) CreateProject() {
project := &format.Project{
Index: 1,
Name: "projectname",
Owner: "root",
IsPrivate: false,
IsMirror: false,
Description: "project description",
CloneURL: "https://example.com/clone",
OriginalURL: "https://example.com/original",
DefaultBranch: "main",
}
f.f.CreateProject(project)
type User struct {
id int64
name string
email string
}
user1 := User{1, "user1", "user1@example.com"}
now := time.Now()
tick := time.Duration(1)
tick++
updated := now.Add(tick)
tick++
closed := now.Add(tick)
tick++
created := now.Add(tick)
issues := []*format.Issue{
{
Number: 1,
PosterID: user1.id,
PosterName: user1.name,
PosterEmail: user1.email,
Title: "title",
Content: "content",
Milestone: "milestone",
State: "closed",
IsLocked: false,
Created: created,
Updated: updated,
Closed: &closed,
Ref: "",
},
}
f.f.CreateIssues(issues...)
tick++
releaseCreated := now.Add(tick)
tick++
releasePublished := now.Add(tick)
tick++
assetCreated := now.Add(tick)
tick++
assetUpdated := now.Add(tick)
size := 50
downloadCount := 10
contentType := "application/zip"
downloadURL := "https://example.com/asset5"
releases := []*format.Release{
{
TagName: "tag",
TargetCommitish: "stable",
Name: "v12 name",
Body: "v12 body",
Draft: false,
Prerelease: false,
PublisherID: user1.id,
PublisherName: user1.name,
PublisherEmail: user1.email,
Assets: []*format.ReleaseAsset{
{
ID: 5,
Name: "asset 5",
ContentType: &contentType,
Size: &size,
DownloadCount: &downloadCount,
Created: assetCreated,
Updated: assetUpdated,
DownloadURL: &downloadURL,
},
},
Created: releaseCreated,
Published: releasePublished,
},
}
f.f.CreateReleases(releases...)
}
[
{
"issue_index": 1,
"index": 5,
"poster_id": 1,
"poster_name": "name_a",
"poster_email": "email_a",
"created": "1985-04-12T23:20:50.52Z",
"updated": "1986-04-12T23:20:50.52Z",
"content": "comment_content_5",
"reactions": null
}
]
[
{
"number": 1,
"poster_id": 1,
"poster_name": "name_a",
"poster_email": "email_a",
"title": "title_a",
"content": "content_a",
"milestone": "",
"state": "closed",
"is_locked": false,
"created": "1985-04-12T23:20:50.52Z",
"updated": "1986-04-12T23:20:50.52Z",
"closed": "1987-04-12T23:20:50.52Z",
"labels": null,
"reactions": null,
"assignees": null,
"ref": ""
}
]
{
"index": 1,
"name": "projectname",
"owner": "root",
"is_private": false,
"is_mirror": true,
"description": "project description",
"clone_url": "https://example.com/clone",
"original_url": "https://example.com/original",
"default_branch": "main"
}
[
{
"tag_name": "v12",
"target_commitish": "stable",
"name": "v12 name",
"body": "v12 body",
"draft": false,
"prerelease": false,
"publisher_id": 1,
"publisher_name": "name_1",
"publisher_email": "email_1",
"assets": [
{
"id": 5,
"name": "asset_5",
"content_type": "application/zip",
"size": 50,
"download_count": 10,
"created": "1985-04-12T23:20:50.52Z",
"updated": "1986-04-12T23:20:50.52Z",
"download_url": "url_asset_5"
}
],
"created": "1985-04-12T23:20:50.52Z",
"published": "1985-04-12T23:20:50.52Z"
}
]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment