os: implement and smoketest os.Clearenv
Этот коммит содержится в:
		
							родитель
							
								
									e4f2b9c003
								
							
						
					
					
						коммит
						51fc78c100
					
				
					 4 изменённых файлов: 49 добавлений и 0 удалений
				
			
		| 
						 | 
					@ -25,6 +25,11 @@ func Unsetenv(key string) error {
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Clearenv deletes all environment variables.
 | 
				
			||||||
 | 
					func Clearenv() {
 | 
				
			||||||
 | 
						syscall.Clearenv()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func LookupEnv(key string) (string, bool) {
 | 
					func LookupEnv(key string) (string, bool) {
 | 
				
			||||||
	return syscall.Getenv(key)
 | 
						return syscall.Getenv(key)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -46,6 +46,34 @@ func TestUnsetenv(t *testing.T) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestClearenv(t *testing.T) {
 | 
				
			||||||
 | 
						const testKey = "GO_TEST_CLEARENV"
 | 
				
			||||||
 | 
						const testValue = "1"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// reset env
 | 
				
			||||||
 | 
						defer func(origEnv []string) {
 | 
				
			||||||
 | 
							for _, pair := range origEnv {
 | 
				
			||||||
 | 
								// Environment variables on Windows can begin with =
 | 
				
			||||||
 | 
								// https://blogs.msdn.com/b/oldnewthing/archive/2010/05/06/10008132.aspx
 | 
				
			||||||
 | 
								i := strings.Index(pair[1:], "=") + 1
 | 
				
			||||||
 | 
								if err := Setenv(pair[:i], pair[i+1:]); err != nil {
 | 
				
			||||||
 | 
									t.Errorf("Setenv(%q, %q) failed during reset: %v", pair[:i], pair[i+1:], err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}(Environ())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err := Setenv(testKey, testValue); err != nil {
 | 
				
			||||||
 | 
							t.Fatalf("Setenv(%q, %q) failed: %v", testKey, testValue, err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if _, ok := LookupEnv(testKey); !ok {
 | 
				
			||||||
 | 
							t.Errorf("Setenv(%q, %q) didn't set $%s", testKey, testValue, testKey)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						Clearenv()
 | 
				
			||||||
 | 
						if val, ok := LookupEnv(testKey); ok {
 | 
				
			||||||
 | 
							t.Errorf("Clearenv() didn't clear $%s, remained with value %q", testKey, val)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestLookupEnv(t *testing.T) {
 | 
					func TestLookupEnv(t *testing.T) {
 | 
				
			||||||
	const smallpox = "SMALLPOX"      // No one has smallpox.
 | 
						const smallpox = "SMALLPOX"      // No one has smallpox.
 | 
				
			||||||
	value, ok := LookupEnv(smallpox) // Should not exist.
 | 
						value, ok := LookupEnv(smallpox) // Should not exist.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -151,6 +151,17 @@ func Unsetenv(key string) (err error) {
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func Clearenv() {
 | 
				
			||||||
 | 
						for _, s := range Environ() {
 | 
				
			||||||
 | 
							for j := 0; j < len(s); j++ {
 | 
				
			||||||
 | 
								if s[j] == '=' {
 | 
				
			||||||
 | 
									Unsetenv(s[0:j])
 | 
				
			||||||
 | 
									break
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
 | 
					func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) {
 | 
				
			||||||
	addr := libc_mmap(nil, uintptr(length), int32(prot), int32(flags), int32(fd), uintptr(offset))
 | 
						addr := libc_mmap(nil, uintptr(length), int32(prot), int32(flags), int32(fd), uintptr(offset))
 | 
				
			||||||
	if addr == unsafe.Pointer(^uintptr(0)) {
 | 
						if addr == unsafe.Pointer(^uintptr(0)) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -77,6 +77,11 @@ func Unsetenv(key string) (err error) {
 | 
				
			||||||
	return ENOSYS
 | 
						return ENOSYS
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func Clearenv() (err error) {
 | 
				
			||||||
 | 
						// stub for now
 | 
				
			||||||
 | 
						return ENOSYS
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Environ() []string {
 | 
					func Environ() []string {
 | 
				
			||||||
	env := runtime_envs()
 | 
						env := runtime_envs()
 | 
				
			||||||
	envCopy := make([]string, len(env))
 | 
						envCopy := make([]string, len(env))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Загрузка…
	
	Создание таблицы
		
		Сослаться в новой задаче