Skip to content

Commit

Permalink
gogensig:mv testWritePkgFilesFail
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Dec 23, 2024
1 parent 77f0ad8 commit bb6d7a9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
33 changes: 33 additions & 0 deletions cmd/gogensig/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,39 @@ type Foo struct {
}
}

func TestWritePkgFilesFail(t *testing.T) {
tempDir, err := os.MkdirTemp(dir, "test_package_write_unwritable")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)
converter, err := convert.NewAstConvert(&convert.AstConvertConfig{
PkgName: "test",
SymbFile: "",
CfgFile: "",
OutputDir: tempDir,
})
if err != nil {
t.Fatal("NewAstConvert Fail")
}
err = os.Chmod(tempDir, 0555)
defer func() {
if err := os.Chmod(tempDir, 0755); err != nil {
t.Fatalf("Failed to change directory permissions: %v", err)
}
}()
if err != nil {
t.Fatalf("Failed to change directory permissions: %v", err)
}
converter.VisitStart("test.h", "/path/to/test.h", false)
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic, but got: %v", r)
}
}()
converter.WritePkgFiles()
}

func TestGetIncPathFail(t *testing.T) {
cfg, err := config.CreateTmpJSONFile("llcppg.cfg", &cppgtypes.Config{
Include: []string{"unexist.h"},
Expand Down
3 changes: 0 additions & 3 deletions cmd/gogensig/convert/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,6 @@ func (p *Package) WritePkgFiles() error {
//
// Files that are already processed in dependent packages will not be output.
func (p *Package) Write(headerFile string) error {
if p.curFile.isSys {
return nil
}
fileName := names.HeaderFileToGo(headerFile)
filePath := filepath.Join(p.GetOutputDir(), fileName)
if debug {
Expand Down
27 changes: 3 additions & 24 deletions cmd/gogensig/convert/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,30 +307,9 @@ func TestPackageWrite(t *testing.T) {
}
})

t.Run("UnwritableOutputDir", func(t *testing.T) {
tempDir, err := os.MkdirTemp(dir, "test_package_write_unwritable")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)

pkg := createTestPkg(t, &convert.PackageConfig{
OutputDir: tempDir,
})

// read-only
err = os.Chmod(tempDir, 0555)
defer func() {
if err := os.Chmod(tempDir, 0755); err != nil {
t.Fatalf("Failed to change directory permissions: %v", err)
}
}()
if err != nil {
t.Fatalf("Failed to change directory permissions: %v", err)
}

pkg.SetCurFile(incPath, incPath, true, true, false)
err = pkg.WritePkgFiles()
t.Run("WriteUnexistFile", func(t *testing.T) {
pkg := createTestPkg(t, &convert.PackageConfig{})
err := pkg.Write("test1.h")
if err == nil {
t.Fatal("Expected an error for invalid output directory, but got nil")
}
Expand Down

0 comments on commit bb6d7a9

Please sign in to comment.