Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #148 from scorphus/amendments
Browse files Browse the repository at this point in the history
Amendments
  • Loading branch information
andrewsmedina committed Aug 11, 2014
2 parents 3497975 + 91e7234 commit 86f4de4
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 32 deletions.
6 changes: 3 additions & 3 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (
"strings"
)

var maxMemory int
var maxMemory uint

func maxMemoryValue() int {
func maxMemoryValue() uint {
if maxMemory > 0 {
return maxMemory
}
var err error
maxMemory, err = config.GetInt("api:request:maxMemory")
maxMemory, err = config.GetUint("api:request:maxMemory")
if err != nil {
panic("You should configure a api:request:maxMemory for gandalf.")
}
Expand Down
24 changes: 13 additions & 11 deletions api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,22 @@ func (s *S) authKeysContent(c *gocheck.C) string {

func (s *S) TestMaxMemoryValueShouldComeFromGandalfConf(c *gocheck.C) {
config.Set("api:request:maxMemory", 1024)
oldMaxMemory := maxMemory
maxMemory = 0
c.Assert(maxMemoryValue(), gocheck.Equals, 1024)
defer func() {
maxMemory = oldMaxMemory
}()
c.Assert(maxMemoryValue(), gocheck.Equals, uint(1024))
}

func (s *S) TestMaxMemoryValueDontResetMaxMemory(c *gocheck.C) {
config.Set("api:request:maxMemory", 1024)
oldMaxMemory := maxMemory
maxMemory = 359
c.Assert(maxMemoryValue(), gocheck.Equals, 359)
defer func() {
maxMemory = oldMaxMemory
}()
c.Assert(maxMemoryValue(), gocheck.Equals, uint(359))
}

func (s *S) TestNewUser(c *gocheck.C) {
Expand Down Expand Up @@ -1295,9 +1303,7 @@ func (s *S) TestPostNewCommit(c *gocheck.C) {
"committer-email": "doge@much.com",
"branch": "master",
}
var files = []struct {
Name, Body string
}{
var files = []multipartzip.File{
{"doge.txt", "Much doge"},
{"much.txt", "Much mucho"},
{"WOW/WOW.WOW", "WOW\nWOW"},
Expand Down Expand Up @@ -1372,9 +1378,7 @@ func (s *S) TestPostNewCommitWithoutBranch(c *gocheck.C) {
"committer-name": "Doge Dog",
"committer-email": "doge@much.com",
}
var files = []struct {
Name, Body string
}{
var files = []multipartzip.File{
{"doge.txt", "Much doge"},
{"much.txt", "Much mucho"},
{"WOW/WOW.WOW", "WOW\nWOW"},
Expand Down Expand Up @@ -1405,9 +1409,7 @@ func (s *S) TestPostNewCommitWithEmptyBranch(c *gocheck.C) {
"committer-email": "doge@much.com",
"branch": "",
}
var files = []struct {
Name, Body string
}{
var files = []multipartzip.File{
{"doge.txt", "Much doge"},
{"much.txt", "Much mucho"},
{"WOW/WOW.WOW", "WOW\nWOW"},
Expand Down
2 changes: 2 additions & 0 deletions etc/gandalf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ uid: git
api:
request:
maxMemory: 2048
repository:
tempDir: /tmp
7 changes: 6 additions & 1 deletion multipartzip/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import (
"path/filepath"
)

func CreateZipBuffer(files []struct{ Name, Body string }) (*bytes.Buffer, error) {
type File struct {
Name string
Body string
}

func CreateZipBuffer(files []File) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
for _, file := range files {
Expand Down
12 changes: 3 additions & 9 deletions multipartzip/multipartzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ func (s *S) TestCopyZipFile(c *gocheck.C) {
os.RemoveAll(tempDir)
}()
c.Assert(err, gocheck.IsNil)
var files = []struct {
Name, Body string
}{
var files = []File{
{"doge.txt", "Much doge"},
{"much.txt", "Much mucho"},
{"WOW/WOW.WOW1", "WOW\nWOW"},
Expand All @@ -55,9 +53,7 @@ func (s *S) TestCopyZipFile(c *gocheck.C) {
func (s *S) TestExtractZip(c *gocheck.C) {
boundary := "muchBOUNDARY"
params := map[string]string{}
var files = []struct {
Name, Body string
}{
var files = []File{
{"doge.txt", "Much doge"},
{"much.txt", "Much mucho"},
{"WOW/WOW.WOW1", "WOW\nWOW"},
Expand Down Expand Up @@ -137,9 +133,7 @@ func (s *S) TestValueFieldWhenFieldEmpty(c *gocheck.C) {
func (s *S) TestFileField(c *gocheck.C) {
boundary := "muchBOUNDARY"
params := map[string]string{}
var files = []struct {
Name, Body string
}{
var files = []File{
{"doge.txt", "Much doge"},
{"much.txt", "Much mucho"},
{"WOW/WOW.WOW1", "WOW\nWOW"},
Expand Down
24 changes: 19 additions & 5 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ import (
"strings"
)

var tempDir string

func tempDirLocation() string {
if tempDir != "" {
return tempDir
}
var err error
tempDir, err = config.GetString("repository:tempDir")
if err != nil {
panic("You should configure a repository:tempDir for gandalf.")
}
return tempDir
}

// Repository represents a Git repository. A Git repository is a record in the
// database and a directory in the filesystem (the bare repository).
type Repository struct {
Expand Down Expand Up @@ -499,7 +513,7 @@ func (*GitContentRetriever) GetTags(repo string) ([]Ref, error) {
return tags, err
}

func (*GitContentRetriever) TempClone(repo string) (string, func(), error) {
func (*GitContentRetriever) TempClone(repo string) (cloneDir string, cleanUp func(), err error) {
gitPath, err := exec.LookPath("git")
if err != nil {
return "", nil, fmt.Errorf("Error when trying to clone repository %s (%s).", repo, err)
Expand All @@ -509,19 +523,19 @@ func (*GitContentRetriever) TempClone(repo string) (string, func(), error) {
if err != nil || !repoExists {
return "", nil, fmt.Errorf("Error when trying to clone repository %s (Repository does not exist).", repo)
}
cloneDir, err := ioutil.TempDir("", "gandalf_clone")
cloneDir, err = ioutil.TempDir(tempDir, "gandalf_clone")
if err != nil {
return "", nil, fmt.Errorf("Error when trying to clone repository %s (Could not create temporary directory).", repo)
}
cleanup := func() {
cleanUp = func() {
os.RemoveAll(cloneDir)
}
cmd := exec.Command(gitPath, "clone", repoDir, cloneDir)
out, err := cmd.CombinedOutput()
if err != nil {
return cloneDir, cleanup, fmt.Errorf("Error when trying to clone repository %s into %s (%s [%s]).", repo, cloneDir, err, out)
return cloneDir, cleanUp, fmt.Errorf("Error when trying to clone repository %s into %s (%s [%s]).", repo, cloneDir, err, out)
}
return cloneDir, cleanup, nil
return cloneDir, cleanUp, nil
}

func (*GitContentRetriever) SetCommitter(cloneDir string, committer GitUser) error {
Expand Down
24 changes: 21 additions & 3 deletions repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ func (s *S) TearDownSuite(c *gocheck.C) {
conn.User().Database.DropDatabase()
}

func (s *S) TestTempDirLocationShouldComeFromGandalfConf(c *gocheck.C) {
config.Set("repository:tempDir", "/home/gandalf/temp")
oldTempDir := tempDir
tempDir = ""
defer func() {
tempDir = oldTempDir
}()
c.Assert(tempDirLocation(), gocheck.Equals, "/home/gandalf/temp")
}

func (s *S) TestTempDirLocationDontResetTempDir(c *gocheck.C) {
config.Set("repository:tempDir", "/home/gandalf/temp")
oldTempDir := tempDir
tempDir = "/var/folders"
defer func() {
tempDir = oldTempDir
}()
c.Assert(tempDirLocation(), gocheck.Equals, "/var/folders")
}

func (s *S) TestNewShouldCreateANewRepository(c *gocheck.C) {
tmpdir, err := commandmocker.Add("git", "$*")
c.Assert(err, gocheck.IsNil)
Expand Down Expand Up @@ -1615,9 +1635,7 @@ func (s *S) TestCommitZipIntegration(c *gocheck.C) {
c.Assert(errCreate, gocheck.IsNil)
boundary := "muchBOUNDARY"
params := map[string]string{}
var files = []struct {
Name, Body string
}{
var files = []multipartzip.File{
{"doge.txt", "Much doge"},
{"much.txt", "Much mucho"},
{"much/WOW.txt", "Much WOW"},
Expand Down

0 comments on commit 86f4de4

Please sign in to comment.