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

Gop_Env #107

Merged
merged 2 commits into from
Feb 25, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions bufiox/bufio.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2020 Qiniu Limited (qiniu.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bufiox

import (
Expand All @@ -10,13 +26,13 @@ import (

// -----------------------------------------------------------------------------

type nilReaderImpl int
type nilReaderImpl struct{}

func (p nilReaderImpl) Read(b []byte) (n int, err error) {
return 0, io.EOF
}

var nilReader io.Reader = nilReaderImpl(0)
var nilReader io.Reader = nilReaderImpl{}

// -----------------------------------------------------------------------------

Expand Down Expand Up @@ -82,7 +98,6 @@ var ErrSeekUnsupported = errors.New("bufio: the underlying reader doesn't suppor
// SeekCurrent means relative to the current offset, and SeekEnd means
// relative to the end. Seek returns the new offset relative to the start
// of the file and an error, if any.
//
func Seek(b *bufio.Reader, offset int64, whence int) (int64, error) {
r := getUnderlyingReader(b)
if seeker, ok := r.(io.Seeker); ok {
Expand Down
27 changes: 24 additions & 3 deletions bufiox/bufio_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2020 Qiniu Limited (qiniu.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bufiox

import (
Expand All @@ -13,12 +29,17 @@ import (

func TestReaderSize(t *testing.T) {
b1 := NewReaderBuffer(nil)
size1 := unsafe.Sizeof(*b1)
b2 := bufio.NewReader(os.Stdin)
size2 := unsafe.Sizeof(*b2)
if size1 != size2 {
_ = b2
if unsafe.Sizeof(*b1) != unsafe.Sizeof(*b2) {
t.Fatal("TestReaderSize: sizeof(bufiox.Reader) != sizeof(bufio.Reader)")
}
if Buffer(b1) != nil {
t.Fatal("Buffer not nil")
}
if !IsReaderBuffer(b1) {
t.Fatal("not IsReaderBuffer?")
}
b, err := ReadAll(b1)
if err != nil || b != nil {
t.Fatal("ReadAll failed:", err, b)
Expand Down
18 changes: 16 additions & 2 deletions bufiox/seek.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2020 Qiniu Limited (qiniu.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bufiox

import (
Expand All @@ -24,7 +40,6 @@ func NewReader(rd io.ReadSeeker) *Reader {
// NewReaderSize returns a new Reader whose buffer has at least the specified
// size. If the argument io.Reader is already a Reader with large enough size,
// it returns the underlying Reader.
//
func NewReaderSize(rd io.ReadSeeker, size int) *Reader {
b, ok := rd.(*Reader)
if ok && b.Size() >= size {
Expand All @@ -39,7 +54,6 @@ func NewReaderSize(rd io.ReadSeeker, size int) *Reader {
// SeekCurrent means relative to the current offset, and SeekEnd means
// relative to the end. Seek returns the new offset relative to the start
// of the file and an error, if any.
//
func (r *Reader) Seek(offset int64, whence int) (int64, error) {
return Seek(&r.Reader, offset, whence)
}
Expand Down
5 changes: 5 additions & 0 deletions gsh/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func (p *App) initApp() {
p.ferr = os.Stderr
}

// Gop_Env retrieves the value of the environment variable named by the key.
func (p *App) Gop_Env(key string) string {
return Sys.Getenv(key)
}

func (p *App) execWith(env []string, name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Stdin = p.fin
Expand Down
9 changes: 9 additions & 0 deletions gsh/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ type OS interface {
// variables are replaced by the empty string.
ExpandEnv(s string) string

// Getenv retrieves the value of the environment variable named by the key.
// It returns the value, which will be empty if the variable is not present.
// To distinguish between an empty value and an unset value, use LookupEnv.
Getenv(key string) string

// Run starts the specified command and waits for it to complete.
Run(c *exec.Cmd) error
}
Expand All @@ -102,6 +107,10 @@ func (p defaultOS) ExpandEnv(s string) string {
return os.ExpandEnv(s)
}

func (p defaultOS) Getenv(key string) string {
return os.Getenv(key)
}

func (p defaultOS) Run(c *exec.Cmd) error {
return c.Run()
}
Expand Down
8 changes: 8 additions & 0 deletions gsh/gsh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (p mockOS) ExpandEnv(s string) string {
})
}

func (p mockOS) Getenv(key string) string {
return Getenv(mockEnv, key)
}

func (p mockOS) Run(c *exec.Cmd) error {
if mockEcho {
fmt.Fprintln(c.Stdout, c.Env, c.Args)
Expand Down Expand Up @@ -91,6 +95,7 @@ func TestOS(t *testing.T) {
var sys defaultOS
sys.Environ()
sys.ExpandEnv("foo")
sys.Getenv("foo")
sys.Run(new(exec.Cmd))
}

Expand All @@ -106,6 +111,9 @@ func TestEnv(t *testing.T) {
func TestExecWithEnv(t *testing.T) {
var app App
app.initApp()
if v := app.Gop_Env("BAR"); v != "bar" {
t.Fatal("app.Gop_Env:", v)
}
capout(&app, func() {
err := app.Exec__0(M{"FOO": "123"}, "./app", "$FOO")
check(t, err)
Expand Down