Skip to content

Commit

Permalink
Adds a testing-files git repo as a submodule to represent a
Browse files Browse the repository at this point in the history
local git repository, and some tests for that functionality.
  • Loading branch information
gwynforthewyn committed Aug 23, 2023
1 parent ba79fc3 commit 555e866
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "testing-files"]
path = testing-files
url = https://github.com/PlayTechnique/templ-testing-files.git
178 changes: 178 additions & 0 deletions repository/repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package repository_test

import (
"bytes"
"errors"
"fmt"
"log"
"os"
"templ/repository"
"testing"
)

//
//type gitGoMock struct {
// mock.Mock
//}
//
//func (g gitGoMock) PlainClone {
//
//}

var testLogger *log.Logger
var buf bytes.Buffer

func init() {
testLogger = log.New(&buf, "repositoryTest: ", log.Lshortfile)
}

func TestRepositoryConstructorWithEmptyUrl(t *testing.T) {
_, err := repository.NewGitRepository("")

if !errors.Is(err, repository.ErrInvalidUpstream{}) {
t.Errorf("expected error of type ErrInvalidUpstream, got: %v", err)
}
}

func TestGithubConstructorWithEmptyUrl(t *testing.T) {
_, err := repository.NewGitHubRepository("")

if !errors.Is(err, repository.ErrInvalidUpstream{}) {
t.Errorf("expected error of type ErrInvalidUpstream, got: %v", err)
}
}

func TestLocalGitConstructorWithEmptyUrl(t *testing.T) {
_, err := repository.NewLocalGitRepository("")

if !errors.Is(err, repository.ErrInvalidUpstream{}) {
t.Errorf("expected error of type ErrInvalidUpstream, got: %v", err)
}
}

func TestLocalGitDestination(t *testing.T) {
tempDir := setupTemplDir("templ-local-github-destination", t)
defer cleanUpTemplDir(tempDir, t)

// The current working directory is the directory repository_test.go is in.
localTestRepo := "../testing-files/local-git-repo"
destDir := fmt.Sprintf("%s/local/local-git-repo", tempDir)

gitRepo, err := repository.NewLocalGitRepository(localTestRepo)
if err != nil {
t.Errorf("could not find %s: %v", localTestRepo, err)
}

if gitRepo.TemplDestination() != destDir {
t.Errorf("local repo templ destination is %s, should be %s", gitRepo.TemplDestination(), destDir)
}

}

func TestGithubDestination(t *testing.T) {
tempDir := setupTemplDir("templ-local-github-destination", t)
defer cleanUpTemplDir(tempDir, t)

// The current working directory is the directory repository_test.go is in.
destDir := fmt.Sprintf("%s/github/PlayTechnique/templ_templates", tempDir)

gitRepo, err := repository.NewGitHubRepository("https://github.com/PlayTechnique/templ_templates.git")

if err != nil {
t.Errorf("error constructing GithubRepositoryfrom %s: %v", gitRepo, err)
}

if gitRepo.TemplDestination() != destDir {
t.Errorf("github repo templ destination is %s, should be %s", gitRepo.TemplDestination(), destDir)
}

}

//func TestLocalGithubFetch(t *testing.T) {
// tempDir := setupTemplDir("templ-testing-local-github-fetch", t)
// defer cleanUpTemplDir(tempDir, t)
//
// // The current working directory is the directory repository_test.go is in.
// localTestRepo := "../testing-files/local-git-repo"
//
// gitRepo, err := repository.NewLocalGitRepository(localTestRepo)
//
// if err != nil {
// t.Errorf("could not find %s: %v", localTestRepo, err)
// }
//
// err = gitRepo.Fetch()
//
// if err != nil {
// t.Errorf("failure in local git repo fetching %v", err)
// }
//
// expectedDestination := "local/local-git-repo/"
// info, err := os.Stat(expectedDestination)
//
// if err != nil {
// t.Errorf("failure in local git repo checking the destination %s: %v", expectedDestination, err)
// }
//
// if !info.IsDir() {
// t.Errorf("%s was attempted to fetch into %s, but the destination is not a directory", gitRepo.Origin(), expectedDestination)
// }
//
//}

// Creates a temporary directory and sets the TEMPL_DIR environment variable.
func setupTemplDir(pattern string, t *testing.T) string {
tempDir, err := os.MkdirTemp("", pattern)

testLogger.Printf("repository test dir is %s", tempDir)
fmt.Print(&buf)

if err != nil {
t.Errorf("could not create temp dir")
}

err = os.Setenv("TEMPL_DIR", tempDir)

if err != nil {
t.Errorf("error setting environment variable TEMPL_DIR")
}

return tempDir
}

func cleanUpTemplDir(tempDir string, t *testing.T) {
err := os.Unsetenv("TEMPL_DIR")

if err != nil {
t.Errorf("could not unset TEMPL_DIR env var: %v", err)
}

err = os.RemoveAll(tempDir)

if err != nil {
t.Errorf("could not remove temp dir: %v", err)
}
}

//func TestGithubFetchWithInvalidUrl(t *testing.T) {
// ghub, err := repository.NewGitRepository("roflcopter")
// if err != nil {
// t.Errorf(")
// }
//
// err := ghub.Fetch()
//
// if _, ok := err.(repository.ErrInvalidUpstream); !ok {
// t.Error("Expected ErrInvalidUpstream error, got %v", err)
// }
//
// err = ghub.Fetch("http://example.com")
//
// if _, ok := err.(repository.ErrInvalidUpstream); !ok {
// t.Error("Expected ErrInvalidUpstream error, got %v", err)
// }
//}

//func TestGithubFetchWithValidUrl(t *testing.T) {
//
//}
1 change: 1 addition & 0 deletions testing-files
Submodule testing-files added at ab42a8

0 comments on commit 555e866

Please sign in to comment.