Skip to content

Commit

Permalink
Merge branch 'feat/purge-build-cache' of github.com:arduino/arduino-c…
Browse files Browse the repository at this point in the history
…li into feat/purge-build-cache
  • Loading branch information
Luca Bianconi committed Feb 7, 2023
2 parents bf58720 + 9f3de10 commit 5296056
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
35 changes: 21 additions & 14 deletions buildcache/build_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,32 @@ import (
"github.com/sirupsen/logrus"
)

type wrapError struct {
wrapped error
}
const (
createDirErrCode = 1
fileWriteErrCode = 2
)

func (e wrapError) Error() string {
return e.wrapped.Error()
type cacheError struct {
Code int
wrappedErr error
}

func (e wrapError) Unwrap() error {
return e.wrapped
func (e cacheError) Error() string {
return e.wrappedErr.Error()
}

type ErrCreateBaseDir struct {
wrapError
}
type ErrWriteLastUsedFile struct {
wrapError
func (e cacheError) Is(target error) bool {
te, ok := target.(cacheError)
return ok && te.Code == e.Code
}

var (
// CreateDirErr error occurred when creating the cache directory
CreateDirErr = cacheError{Code: createDirErrCode}
// FileWriteErr error occurred when writing the placeholder file
FileWriteErr = cacheError{Code: fileWriteErrCode}
)

const lastUsedFileName = ".last-used"

// BuildCache represents a cache of built files (sketches and cores), it's designed
Expand All @@ -56,11 +63,11 @@ type BuildCache struct {
func (bc *BuildCache) GetOrCreate(key string) (*paths.Path, error) {
keyDir := bc.baseDir.Join(key)
if err := keyDir.MkdirAll(); err != nil {
return nil, &ErrCreateBaseDir{wrapError{err}}
return nil, cacheError{createDirErrCode, err}
}

if err := keyDir.Join(lastUsedFileName).WriteFile([]byte{}); err != nil {
return nil, &ErrWriteLastUsedFile{wrapError{err}}
return nil, cacheError{fileWriteErrCode, err}
}
return keyDir, nil
}
Expand Down
7 changes: 3 additions & 4 deletions commands/sketch/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,13 @@ func validateSketchName(name string) error {
return &arduino.CantCreateSketchError{Cause: errors.New(tr("sketch name cannot be empty"))}
}
if len(name) > sketchNameMaxLength {
return &arduino.CantCreateSketchError{Cause: errors.New(tr("sketch name too long (%d characters). Maximum allowed length is %d",
return &arduino.CantCreateSketchError{Cause: errors.New(tr("sketch name too long (%[1]d characters). Maximum allowed length is %[2]d",
len(name),
sketchNameMaxLength))}
}
if !sketchNameValidationRegex.MatchString(name) {
return &arduino.CantCreateSketchError{Cause: errors.New(tr("invalid sketch name \"%s\". Required pattern %s",
name,
sketchNameValidationRegex.String()))}
return &arduino.CantCreateSketchError{Cause: errors.New(tr(`invalid sketch name "%[1]s": the first character must be alphanumeric, the following ones can also contain "_", "-", and ".".`,
name))}
}
return nil
}
16 changes: 6 additions & 10 deletions commands/sketch/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sketch

import (
"context"
"fmt"
"testing"

"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand All @@ -11,7 +12,6 @@ import (
func Test_SketchNameWrongPattern(t *testing.T) {
invalidNames := []string{
"&",
"",
".hello",
"_hello",
"-hello",
Expand All @@ -24,11 +24,9 @@ func Test_SketchNameWrongPattern(t *testing.T) {
SketchName: name,
SketchDir: t.TempDir(),
})
require.NotNil(t, err)

require.Error(t, err, `Can't create sketch: invalid sketch name "%s". Required pattern %s`,
name,
sketchNameValidationRegex)
require.EqualError(t, err, fmt.Sprintf(`Can't create sketch: invalid sketch name "%s": the first character must be alphanumeric, the following ones can also contain "_", "-", and ".".`,
name))
}
}

Expand All @@ -38,9 +36,8 @@ func Test_SketchNameEmpty(t *testing.T) {
SketchName: emptyName,
SketchDir: t.TempDir(),
})
require.NotNil(t, err)

require.Error(t, err, `Can't create sketch: sketch name cannot be empty`)
require.EqualError(t, err, `Can't create sketch: sketch name cannot be empty`)
}

func Test_SketchNameTooLong(t *testing.T) {
Expand All @@ -52,11 +49,10 @@ func Test_SketchNameTooLong(t *testing.T) {
SketchName: string(tooLongName),
SketchDir: t.TempDir(),
})
require.NotNil(t, err)

require.Error(t, err, `Can't create sketch: sketch name too long (%d characters). Maximum allowed length is %d`,
require.EqualError(t, err, fmt.Sprintf(`Can't create sketch: sketch name too long (%d characters). Maximum allowed length is %d`,
len(tooLongName),
sketchNameMaxLength)
sketchNameMaxLength))
}

func Test_SketchNameOk(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/phases/core_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
targetArchivedCore = buildCachePath.Join(archivedCoreName, "core.a")
_, buildCacheErr = buildcache.New(buildCachePath).GetOrCreate(archivedCoreName)

if errors.As(buildCacheErr, &buildcache.ErrCreateBaseDir{}) {
if errors.Is(buildCacheErr, buildcache.CreateDirErr) {
return nil, nil, fmt.Errorf(tr("creating core cache folder: %s", err))
}

Expand Down

0 comments on commit 5296056

Please sign in to comment.