Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions commands/cmderrors/cmderrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,25 @@ func (e *NotFoundError) GRPCStatus() *status.Status {
return status.New(codes.NotFound, e.Error())
}

// AlreadyExists is returned when creating a resource failed because one already exists
type AlreadyExists struct {
Message string
Cause error
}

func (e *AlreadyExists) Error() string {
return composeErrorMsg(e.Message, e.Cause)
}

func (e *AlreadyExists) Unwrap() error {
return e.Cause
}

// GRPCStatus converts the error into a *status.Status
func (e *AlreadyExists) GRPCStatus() *status.Status {
return status.New(codes.AlreadyExists, e.Error())
}

// PermissionDeniedError is returned when a resource cannot be accessed or modified
type PermissionDeniedError struct {
Message string
Expand Down
2 changes: 1 addition & 1 deletion commands/service_sketch_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *arduinoCoreServerImpl) ArchiveSketch(ctx context.Context, req *rpc.Arch

if !req.GetOverwrite() {
if archivePath.Exist() {
return nil, &cmderrors.InvalidArgumentError{Message: i18n.Tr("Archive already exists")}
return nil, &cmderrors.AlreadyExists{Message: i18n.Tr("Archive already exists")}
}
}

Expand Down
14 changes: 14 additions & 0 deletions internal/integrationtest/arduino-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,17 @@ func (inst *ArduinoCLIInstance) NewSketch(ctx context.Context, sketchName, sketc
logCallf(">>> NewSketch(%+v)\n", req)
return inst.cli.daemonClient.NewSketch(ctx, req)
}

// ArchiveSketch calls the "ArchiveSketch" gRPC method.
func (cli *ArduinoCLI) ArchiveSketch(ctx context.Context, sketchPath, archivePath string, includeBuildDir, overwrite bool) (*commands.ArchiveSketchResponse, error) {
req := &commands.ArchiveSketchRequest{
SketchPath: sketchPath,
ArchivePath: archivePath,
IncludeBuildDir: includeBuildDir,
Overwrite: overwrite,
}
logCallf(">>> ArchiveSketch(%+v)\n", req)
resp, err := cli.daemonClient.ArchiveSketch(ctx, req)
logCallf("err=%v\n", err)
return resp, err
}
22 changes: 22 additions & 0 deletions internal/integrationtest/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,28 @@ func TestDaemonCreateSketch(t *testing.T) {
require.NoError(t, err)
}

func TestDaemonArchiveSketchAlreadyExists(t *testing.T) {
env, cli := integrationtest.CreateEnvForDaemon(t)
defer env.CleanUp()

sketchDir := cli.CopySketch("sketch_simple")
archivePath := cli.WorkingDir().Join("ArchiveSketchAlreadyExists.zip")
if archivePath.Exist() {
require.NoError(t, archivePath.Remove())
}
t.Cleanup(func() { _ = archivePath.Remove() })

_, err := cli.ArchiveSketch(context.Background(), sketchDir.String(), archivePath.String(), false, false)
require.NoError(t, err)

_, err = cli.ArchiveSketch(context.Background(), sketchDir.String(), archivePath.String(), false, false)
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
require.Equal(t, codes.AlreadyExists, st.Code())
require.Contains(t, st.Message(), "Archive already exists")
}

func analyzeUpdateIndexClient(t *testing.T, cl commands.ArduinoCoreService_UpdateIndexClient) (map[string]*commands.DownloadProgressEnd, error) {
analyzer := NewDownloadProgressAnalyzer(t)
for {
Expand Down