/ components / execd / pkg / runtime / workingdir_test.go
workingdir_test.go
 1  // Copyright 2026 Alibaba Group Holding Ltd.
 2  //
 3  // Licensed under the Apache License, Version 2.0 (the "License");
 4  // you may not use this file except in compliance with the License.
 5  // You may obtain a copy of the License at
 6  //
 7  //     http://www.apache.org/licenses/LICENSE-2.0
 8  //
 9  // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  
15  package runtime
16  
17  import (
18  	"os"
19  	"path/filepath"
20  	"testing"
21  
22  	"github.com/stretchr/testify/require"
23  )
24  
25  func TestValidateWorkingDir_empty(t *testing.T) {
26  	require.NoError(t, ValidateWorkingDir(""))
27  }
28  
29  func TestValidateWorkingDir_notExist(t *testing.T) {
30  	tmp := t.TempDir()
31  	missing := filepath.Join(tmp, "definitely-missing-subdir")
32  	err := ValidateWorkingDir(missing)
33  	require.Error(t, err)
34  	require.Contains(t, err.Error(), "does not exist")
35  	require.Contains(t, err.Error(), missing)
36  }
37  
38  func TestValidateWorkingDir_notDir(t *testing.T) {
39  	tmp := t.TempDir()
40  	f := filepath.Join(tmp, "file")
41  	require.NoError(t, os.WriteFile(f, []byte("x"), 0o600))
42  	err := ValidateWorkingDir(f)
43  	require.Error(t, err)
44  	require.Contains(t, err.Error(), "not a directory")
45  }
46  
47  func TestValidateWorkingDir_ok(t *testing.T) {
48  	require.NoError(t, ValidateWorkingDir(t.TempDir()))
49  }
50  
51  func TestValidateWorkingDir_ExpandsHome(t *testing.T) {
52  	home := t.TempDir()
53  	target := filepath.Join(home, "workspace")
54  	require.NoError(t, os.MkdirAll(target, 0o755))
55  	t.Setenv("HOME", home)
56  	t.Setenv("USERPROFILE", home)
57  
58  	require.NoError(t, ValidateWorkingDir("~/workspace"))
59  }