cleanup: move legacy from internal/engine to internal (#759)
No functional change. See https://github.com/ooni/probe/issues/2115
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
# Package github.com/ooni/probe-engine/legacy
|
||||
|
||||
This package contains legacy code that we will soon remove. When this folder
|
||||
is empty, it means there's no large, incremental refactoring in progress.
|
||||
@@ -1,62 +0,0 @@
|
||||
// Package assetsdir contains code to cleanup the assets dir. We removed
|
||||
// the assetsdir in the 3.9.0 development cycle.
|
||||
package assetsdir
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// ErrEmptyDir indicates that you passed to Cleanup an empty dir.
|
||||
var ErrEmptyDir = errors.New("empty assets directory")
|
||||
|
||||
// Result is the result of a Cleanup run.
|
||||
type Result struct {
|
||||
// ASNDatabaseErr is the error of deleting the
|
||||
// file containing the old ASN database.
|
||||
ASNDatabaseErr error
|
||||
|
||||
// CABundleErr is the error of deleting the file
|
||||
// containing the old CA bundle.
|
||||
CABundleErr error
|
||||
|
||||
// CountryDatabaseErr is the error of deleting the
|
||||
// file containing the old country database.
|
||||
CountryDatabaseErr error
|
||||
|
||||
// RmdirErr is the error of deleting the supposedly
|
||||
// empty directory that contained assets.
|
||||
RmdirErr error
|
||||
}
|
||||
|
||||
// Cleanup removes data from the assetsdir. This function will
|
||||
// try to delete the known assets inside of dir. It then also
|
||||
// tries to delete the directory. If the directory is not empty,
|
||||
// this operation will fail. That means the user has put some
|
||||
// extra data in there and we don't want to remove it.
|
||||
//
|
||||
// Returns the Result of cleaning up the assets on success and
|
||||
// an error on failure. The only cause of error is passing to
|
||||
// this function an empty directory. The Result data structure
|
||||
// contains the result of each individual remove operation.
|
||||
func Cleanup(dir string) (*Result, error) {
|
||||
return fcleanup(dir, os.Remove)
|
||||
}
|
||||
|
||||
// fcleanup is a version of Cleanup where we can mock the real function
|
||||
// used for removing files and dirs, so we can write unit tests.
|
||||
func fcleanup(dir string, remove func(name string) error) (*Result, error) {
|
||||
if dir == "" {
|
||||
return nil, ErrEmptyDir
|
||||
}
|
||||
r := &Result{}
|
||||
asndb := filepath.Join(dir, "asn.mmdb")
|
||||
r.ASNDatabaseErr = os.Remove(asndb)
|
||||
cabundle := filepath.Join(dir, "ca-bundle.pem")
|
||||
r.CABundleErr = os.Remove(cabundle)
|
||||
countrydb := filepath.Join(dir, "country.mmdb")
|
||||
r.CountryDatabaseErr = os.Remove(countrydb)
|
||||
r.RmdirErr = os.Remove(dir)
|
||||
return r, nil
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package assetsdir
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCleanupNormalUsage(t *testing.T) {
|
||||
result, err := Cleanup("testdata")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// we expect a bunch of ENOENT because the directory does not exist.
|
||||
isExpectedErr := func(err error) bool {
|
||||
return err != nil && strings.HasSuffix(err.Error(), "no such file or directory")
|
||||
}
|
||||
if !isExpectedErr(result.ASNDatabaseErr) {
|
||||
t.Fatal("unexpected error", result.ASNDatabaseErr)
|
||||
}
|
||||
if !isExpectedErr(result.CABundleErr) {
|
||||
t.Fatal("unexpected error", result.CABundleErr)
|
||||
}
|
||||
if !isExpectedErr(result.CountryDatabaseErr) {
|
||||
t.Fatal("unexpected error", result.CountryDatabaseErr)
|
||||
}
|
||||
if !isExpectedErr(result.RmdirErr) {
|
||||
t.Fatal("unexpected error", result.RmdirErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupWithEmptyInput(t *testing.T) {
|
||||
result, err := Cleanup("")
|
||||
if !errors.Is(err, ErrEmptyDir) {
|
||||
t.Fatal("unexpected error", err)
|
||||
}
|
||||
if result != nil {
|
||||
t.Fatal("expected nil result")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user