Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for wasm/wasip1 #36

Merged
merged 2 commits into from
Oct 28, 2023
Merged
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
24 changes: 24 additions & 0 deletions .github/workflows/test_wasip1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
on: [push, pull_request]
name: Test wasip1
permissions: {}
jobs:
test:
strategy:
matrix:
go-version: [1.21.x]
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: Install wasirun
run: |
go install github.com/stealthrocket/wasi-go/cmd/wasirun@latest
- name: Checkout code
uses: actions/checkout@v3

- name: Test
run: make wasitest
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Go parameters
GOCMD = go
GOTEST = $(GOCMD) test
WASIRUN_WRAPPER := $(CURDIR)/scripts/wasirun-wrapper

.PHONY: test
test:
Expand All @@ -9,3 +10,9 @@ test:
test-coverage:
echo "" > $(COVERAGE_REPORT); \
$(GOTEST) -coverprofile=$(COVERAGE_REPORT) -coverpkg=./... -covermode=$(COVERAGE_MODE) ./...

.PHONY: wasitest
wasitest: export GOARCH=wasm
wasitest: export GOOS=wasip1
wasitest:
$(GOTEST) -exec $(WASIRUN_WRAPPER) ./...
4 changes: 2 additions & 2 deletions osfs/os_bound_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !js
// +build !js
//go:build !wasm
// +build !wasm

/*
Copyright 2022 The Flux authors.
Expand Down
4 changes: 2 additions & 2 deletions osfs/os_chroot_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !js
// +build !js
//go:build !wasm
// +build !wasm

package osfs

Expand Down
4 changes: 2 additions & 2 deletions osfs/os_posix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !plan9 && !windows && !js
// +build !plan9,!windows,!js
//go:build !plan9 && !windows && !wasm
// +build !plan9,!windows,!wasm
pjbgf marked this conversation as resolved.
Show resolved Hide resolved

package osfs

Expand Down
4 changes: 2 additions & 2 deletions osfs/os_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !js
// +build !js
//go:build !wasm
// +build !wasm

package osfs

Expand Down
34 changes: 34 additions & 0 deletions osfs/os_wasip1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build wasip1
// +build wasip1

package osfs

import (
"os"
"syscall"
)

func (f *file) Lock() error {
f.m.Lock()
defer f.m.Unlock()
return nil
}

func (f *file) Unlock() error {
f.m.Lock()
defer f.m.Unlock()
return nil
}

func rename(from, to string) error {
return os.Rename(from, to)
}

// umask sets umask to a new value, and returns a func which allows the
// caller to reset it back to what it was originally.
func umask(new int) func() {
old := syscall.Umask(new)
return func() {
syscall.Umask(old)
}
}
62 changes: 62 additions & 0 deletions osfs/os_wasip1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//go:build wasip1
// +build wasip1

package osfs

import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/test"

. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }

type OSSuite struct {
test.FilesystemSuite
path string
tempCounter int
}

var _ = Suite(&OSSuite{})

func (s *OSSuite) SetUpTest(c *C) {
s.tempCounter++
s.path = fmt.Sprintf("test_%d", s.tempCounter)
s.FilesystemSuite = test.NewFilesystemSuite(New(s.path))
}

func (s *OSSuite) TestOpenDoesNotCreateDir(c *C) {
_, err := s.FS.Open("dir/non-existent")
c.Assert(err, NotNil)

_, err = s.FS.Stat(filepath.Join(s.path, "dir"))
c.Assert(os.IsNotExist(err), Equals, true)
}

func (s *OSSuite) TestCapabilities(c *C) {
_, ok := s.FS.(billy.Capable)
c.Assert(ok, Equals, true)

caps := billy.Capabilities(s.FS)
c.Assert(caps, Equals, billy.DefaultCapabilities)
}

func TestDefault(t *testing.T) {
want := Default
got := Default

if reflect.TypeOf(got) != reflect.TypeOf(want) {
t.Errorf("wanted Default to be %T got %T", want, got)
}
}

func TestNewAPI(t *testing.T) {
_ = New("/")
}
14 changes: 14 additions & 0 deletions scripts/wasirun-wrapper
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
#
# Wrapper script for wasirun to be executable by go test -exec
#

which wasirun || go install github.com/stealthrocket/wasi-go/cmd/wasirun@latest

# Make temp dir for for mapping into wasirun
map_dir=$(mktemp -d)

main_wasm=$1
shift

wasirun --dir=${map_dir} ${main_wasm} -- $*
3 changes: 2 additions & 1 deletion test/common_posix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build !windows
//go:build !windows && !wasip1
// +build !windows,!wasip1

package test

Expand Down
11 changes: 11 additions & 0 deletions test/common_wasip1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build wasip1
// +build wasip1

package test

import "os"

var (
customMode os.FileMode = 0600
expectedSymlinkTarget = "/dir/file"
)
8 changes: 7 additions & 1 deletion test/symlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"os"
"runtime"

. "gopkg.in/check.v1"
. "github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/util"
. "gopkg.in/check.v1"
)

// SymlinkSuite is a convenient test suite to validate any implementation of
Expand Down Expand Up @@ -113,6 +113,9 @@ func (s *SymlinkSuite) TestOpenWithSymlinkToAbsolutePath(c *C) {
if runtime.GOOS == "plan9" {
c.Skip("skipping on Plan 9; symlinks are not supported")
}
if runtime.GOOS == "wasip1" {
c.Skip("skipping on wasip1")
}
err := util.WriteFile(s.FS, "dir/file", []byte("foo"), 0644)
c.Assert(err, IsNil)

Expand Down Expand Up @@ -158,6 +161,9 @@ func (s *SymlinkSuite) TestReadlinkWithAbsolutePath(c *C) {
if runtime.GOOS == "plan9" {
c.Skip("skipping on Plan 9; symlinks are not supported")
}
if runtime.GOOS == "wasip1" {
c.Skip("skipping on wasip1")
}
err := util.WriteFile(s.FS, "dir/file", nil, 0644)
c.Assert(err, IsNil)

Expand Down