Skip to content

Commit

Permalink
fix go removeall
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Tendyck <[email protected]>
  • Loading branch information
thomasten committed Nov 11, 2024
1 parent eb7a5f6 commit 11b3891
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/go
4 changes: 2 additions & 2 deletions src/tests/go/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ add_custom_command(

add_custom_command(
OUTPUT gotest.a
DEPENDS gotest.go
DEPENDS gotest.go removeall.go
COMMAND
sh -c
"GOROOT='${CMAKE_SOURCE_DIR}/../3rdparty/go' '${CMAKE_SOURCE_DIR}/../3rdparty/go/bin/go' build -buildmode=c-archive ${CMAKE_CURRENT_SOURCE_DIR}/gotest.go"
"GOROOT='${CMAKE_SOURCE_DIR}/../3rdparty/go' '${CMAKE_SOURCE_DIR}/../3rdparty/go/bin/go' build -buildmode=c-archive ${CMAKE_CURRENT_SOURCE_DIR}/gotest.go ${CMAKE_CURRENT_SOURCE_DIR}/removeall.go"
)
add_custom_target(gotestlib DEPENDS gotest.a)

Expand Down
5 changes: 5 additions & 0 deletions src/tests/go/gotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func gotest(simulate bool) int32 {
return -10
}

if err := testRemoveAll(); err != nil {
fmt.Println(err)
return -11
}

return 42 // success magic
}

Expand Down
80 changes: 80 additions & 0 deletions src/tests/go/removeall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2024 Edgeless Systems GmbH. All rights reserved.
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"fmt"
"os"
"path/filepath"
)

func testRemoveAll() error {
tmpDir := "/removeall-test"
if err := os.Mkdir(tmpDir, 0o400); err != nil {
return err
}

if err := os.RemoveAll(""); err != nil {
return fmt.Errorf("RemoveAll(\"\"): %v; want nil", err)
}

file := filepath.Join(tmpDir, "file")
path := filepath.Join(tmpDir, "_TestRemoveAll_")
fpath := filepath.Join(path, "file")
dpath := filepath.Join(path, "dir")

// Make a regular file and remove
fd, err := os.Create(file)
if err != nil {
return fmt.Errorf("create %q: %s", file, err)
}
fd.Close()
if err = os.RemoveAll(file); err != nil {
return fmt.Errorf("RemoveAll %q (first): %s", file, err)
}
if _, err = os.Lstat(file); err == nil {
return fmt.Errorf("Lstat %q succeeded after RemoveAll (first)", file)
}

// Make directory with 1 file and remove.
if err := os.MkdirAll(path, 0777); err != nil {
return fmt.Errorf("MkdirAll %q: %s", path, err)
}
fd, err = os.Create(fpath)
if err != nil {
return fmt.Errorf("create %q: %s", fpath, err)
}
fd.Close()
if err = os.RemoveAll(path); err != nil {
return fmt.Errorf("RemoveAll %q (second): %s", path, err)
}
if _, err = os.Lstat(path); err == nil {
return fmt.Errorf("Lstat %q succeeded after RemoveAll (second)", path)
}

// Make directory with file and subdirectory and remove.
if err = os.MkdirAll(dpath, 0777); err != nil {
return fmt.Errorf("MkdirAll %q: %s", dpath, err)
}
fd, err = os.Create(fpath)
if err != nil {
return fmt.Errorf("create %q: %s", fpath, err)
}
fd.Close()
fd, err = os.Create(dpath + "/file")
if err != nil {
return fmt.Errorf("create %q: %s", fpath, err)
}
fd.Close()
if err = os.RemoveAll(path); err != nil {
return fmt.Errorf("RemoveAll %q (third): %s", path, err)
}
if _, err := os.Lstat(path); err == nil {
return fmt.Errorf("Lstat %q succeeded after RemoveAll (third)", path)
}

return nil
}

0 comments on commit 11b3891

Please sign in to comment.