From bb6d7a99d42abc1fc273a16d0ffea5613381e416 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 23 Dec 2024 15:11:34 +0800 Subject: [PATCH] gogensig:mv testWritePkgFilesFail --- cmd/gogensig/convert/convert_test.go | 33 ++++++++++++++++++++++++++++ cmd/gogensig/convert/package.go | 3 --- cmd/gogensig/convert/package_test.go | 27 +++-------------------- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/cmd/gogensig/convert/convert_test.go b/cmd/gogensig/convert/convert_test.go index a6d8df3..8139fbc 100644 --- a/cmd/gogensig/convert/convert_test.go +++ b/cmd/gogensig/convert/convert_test.go @@ -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"}, diff --git a/cmd/gogensig/convert/package.go b/cmd/gogensig/convert/package.go index 1616cf3..fa54344 100644 --- a/cmd/gogensig/convert/package.go +++ b/cmd/gogensig/convert/package.go @@ -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 { diff --git a/cmd/gogensig/convert/package_test.go b/cmd/gogensig/convert/package_test.go index dc60b94..3f82e22 100644 --- a/cmd/gogensig/convert/package_test.go +++ b/cmd/gogensig/convert/package_test.go @@ -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") }