Skip to content

Commit

Permalink
fix: make File interface unambiguous
Browse files Browse the repository at this point in the history
  • Loading branch information
theseion committed Nov 10, 2024
1 parent d0c62ce commit 01b4523
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
4 changes: 2 additions & 2 deletions experimental/corpus/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ type File interface {
// WithCacheDir sets the cache directory
WithCacheDir(cacheDir string) File

// WithFilePath sets the file path
WithFilePath(filePath string) File
// WithFileName sets the filename
WithFileName(fileName string) File
}

// Corpus is the interface that must be implemented to make a corpus available to clients
Expand Down
24 changes: 14 additions & 10 deletions internal/quantitative/leipzig/corpus.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -165,23 +164,22 @@ func (c *LeipzigCorpus) FetchCorpusFile() corpus.File {

url := fmt.Sprintf("%s/%s", c.url_, c.corpusFile)

cacheDir := path.Join(home, ".ftw")
cacheDir := filepath.Join(home, ".ftw")

log.Debug().Msgf("Preparing download of corpus file from %s", url)
dest := path.Join(cacheDir, "extracted")
dest := filepath.Join(cacheDir, "extracted")
if err := os.MkdirAll(dest, os.ModePerm); err != nil {
log.Fatal().Err(err).Msg("Could not create destination directory")
}

cache := NewFile().WithCacheDir(cacheDir).WithFilePath(c.filename)
cache := NewFile().WithCacheDir(cacheDir).WithFileName(c.filename)

if cache.FilePath() == "" {
log.Fatal().Msg("Cache file path is empty")
}

if info, err := os.Stat(path.Join(home, ".ftw", cache.FilePath())); err == nil {
if info, err := os.Stat(cache.FilePath()); err == nil {
log.Debug().Msgf("filename %s already exists", info.Name())
cache = cache.WithFilePath(path.Join(home, ".ftw", c.filename))
return cache
}

Expand Down Expand Up @@ -215,14 +213,20 @@ func (c *LeipzigCorpus) FetchCorpusFile() corpus.File {
log.Trace().Msgf("Checking file %s", info.Name())

if info.Name() == c.filename {
newPath := filepath.Join(cacheDir, info.Name())
err = os.Rename(path, newPath)
if path == cache.FilePath() {
// During tests, concurrent fetching may get us here even though the cache
// file already exists. On Windows this will cause errors because a file can
// only be opened for writing by a single proccess.
log.Info().Msgf("Cache already exists for %s", c.filename)
return filepath.SkipAll
}

err = os.Rename(path, cache.FilePath())
if err != nil {
fmt.Println("Error moving:", err)
return err
}
fmt.Println("Moved", path, "to", newPath)
cache = cache.WithFilePath(newPath)
fmt.Println("Moved", path, "to cache", cache.FilePath())
}

return nil
Expand Down
16 changes: 10 additions & 6 deletions internal/quantitative/leipzig/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@

package leipzig

import "github.com/coreruleset/go-ftw/experimental/corpus"
import (
"path/filepath"

"github.com/coreruleset/go-ftw/experimental/corpus"
)

// File implements the corpus.File interface.
type File struct {
cacheDir string
filePath string
fileName string
}

// NewFile returns a new File
Expand All @@ -23,7 +27,7 @@ func (f File) CacheDir() string {

// FilePath is the path to the cached file
func (f File) FilePath() string {
return f.filePath
return filepath.Join(f.cacheDir, f.fileName)
}

// WithCacheDir sets the cache directory
Expand All @@ -32,8 +36,8 @@ func (f File) WithCacheDir(cacheDir string) corpus.File {
return f
}

// WithFilePath sets the file path
func (f File) WithFilePath(filePath string) corpus.File {
f.filePath = filePath
// WithFileName sets the filename
func (f File) WithFileName(fileName string) corpus.File {
f.fileName = fileName
return f
}
5 changes: 3 additions & 2 deletions internal/quantitative/leipzig/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package leipzig

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/suite"
Expand All @@ -21,6 +22,6 @@ func (s *fileTestSuite) TestFile_CacheDir() {
f := NewFile()
f = f.WithCacheDir("cacheDir")
s.Require().Equal("cacheDir", f.CacheDir())
f = f.WithFilePath("filePath")
s.Require().Equal("filePath", f.FilePath())
f = f.WithFileName("fileName")
s.Require().Equal(filepath.Join("cacheDir", "fileName"), f.FilePath())
}

0 comments on commit 01b4523

Please sign in to comment.