Skip to content

Commit

Permalink
fix: [#345] app.Publishes is not working for directory (#465)
Browse files Browse the repository at this point in the history
* fix: [#345] app.Publishes is not working for directory

* upgrade v1.13.9
  • Loading branch information
hwbrzzl committed Apr 25, 2024
1 parent 388f792 commit d8ad014
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 46 deletions.
65 changes: 50 additions & 15 deletions foundation/console/vendor_publish_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,28 @@ func (receiver *VendorPublishCommand) packageDir(packageName string) (string, er

func (receiver *VendorPublishCommand) publish(sourcePath, targetPath string, existing, force bool) (map[string]string, error) {
result := make(map[string]string)
sourcePathStat, err := os.Stat(sourcePath)
isTargetPathDir := filepath.Ext(targetPath) == ""
isSourcePathDir := filepath.Ext(sourcePath) == ""

sourceFiles, err := receiver.getSourceFiles(sourcePath)
if err != nil {
return nil, err
}

var sourceFiles []string
if sourcePathStat.IsDir() {
fileInfos, err := os.ReadDir(sourcePath)
if err != nil {
return nil, err
}
for _, fileInfo := range fileInfos {
sourceFiles = append(sourceFiles, filepath.Join(sourcePath, fileInfo.Name()))
for _, sourceFile := range sourceFiles {
relativePath := ""
if isSourcePathDir {
relativePath, err = filepath.Rel(sourcePath, sourceFile)
if err != nil {
return nil, err
}
} else {
relativePath = filepath.Base(sourcePath)
}
} else {
sourceFiles = append(sourceFiles, sourcePath)
}

for _, sourceFile := range sourceFiles {
targetFile := targetPath
if filepath.Ext(targetFile) == "" {
targetFile = filepath.Join(targetFile, filepath.Base(sourceFile))
if isTargetPathDir {
targetFile = filepath.Join(targetPath, relativePath)
}

success, err := receiver.publishFile(sourceFile, targetFile, existing, force)
Expand All @@ -191,6 +191,41 @@ func (receiver *VendorPublishCommand) publish(sourcePath, targetPath string, exi
return result, nil
}

func (receiver *VendorPublishCommand) getSourceFiles(sourcePath string) ([]string, error) {
sourcePathStat, err := os.Stat(sourcePath)
if err != nil {
return nil, err
}

if sourcePathStat.IsDir() {
return receiver.getSourceFilesForDir(sourcePath)
} else {
return []string{sourcePath}, nil
}
}

func (receiver *VendorPublishCommand) getSourceFilesForDir(sourcePath string) ([]string, error) {
dirEntries, err := os.ReadDir(sourcePath)
if err != nil {
return nil, err
}

var sourceFiles []string
for _, dirEntry := range dirEntries {
if dirEntry.IsDir() {
sourcePaths, err := receiver.getSourceFilesForDir(filepath.Join(sourcePath, dirEntry.Name()))
if err != nil {
return nil, err
}
sourceFiles = append(sourceFiles, sourcePaths...)
} else {
sourceFiles = append(sourceFiles, filepath.Join(sourcePath, dirEntry.Name()))
}
}

return sourceFiles, nil
}

func (receiver *VendorPublishCommand) publishFile(sourceFile, targetFile string, existing, force bool) (bool, error) {
content, err := os.ReadFile(sourceFile)
if err != nil {
Expand Down
132 changes: 102 additions & 30 deletions foundation/console/vendor_publish_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"

"github.com/stretchr/testify/suite"

"github.com/goravel/framework/support/file"
)

type VendorPublishCommandTestSuite struct {
Expand All @@ -20,6 +22,69 @@ func (s *VendorPublishCommandTestSuite) SetupTest() {

}

func (s *VendorPublishCommandTestSuite) TestGetSourceFiles() {
command := &VendorPublishCommand{}

sourceDir, err := os.MkdirTemp("", "source")
s.Require().Nil(err)
defer func(path string) {
if err := file.Remove(path); err != nil {
panic(err)
}
}(sourceDir)

sourceFile := filepath.Join(sourceDir, "test.txt")
s.Require().Nil(file.Create(sourceFile, "test"))
sourceFile = filepath.Join(sourceDir, "dir1/test.txt")
s.Require().Nil(file.Create(sourceFile, "test"))

files, err := command.getSourceFiles(filepath.Join(sourceDir, "test.txt"))
s.Require().NoError(err)
s.ElementsMatch(files, []string{
filepath.Join(sourceDir, "test.txt"),
})

files, err = command.getSourceFiles(sourceDir)
s.Require().NoError(err)
s.ElementsMatch(files, []string{
filepath.Join(sourceDir, "test.txt"),
filepath.Join(sourceDir, "dir1/test.txt"),
})
}

func (s *VendorPublishCommandTestSuite) TestGetSourceFilesForDir() {
command := &VendorPublishCommand{}

sourceDir, err := os.MkdirTemp("", "source")
s.Require().Nil(err)
defer func(path string) {
if err := file.Remove(path); err != nil {
panic(err)
}
}(sourceDir)

sourceFile := filepath.Join(sourceDir, "test.txt")
s.Require().Nil(file.Create(sourceFile, "test"))
sourceFile = filepath.Join(sourceDir, "test1.txt")
s.Require().Nil(file.Create(sourceFile, "test"))
sourceFile = filepath.Join(sourceDir, "dir1/test.txt")
s.Require().Nil(file.Create(sourceFile, "test"))
sourceFile = filepath.Join(sourceDir, "dir1/dir11/test.txt")
s.Require().Nil(file.Create(sourceFile, "test"))
sourceFile = filepath.Join(sourceDir, "dir2/test.txt")
s.Require().Nil(file.Create(sourceFile, "test"))

files, err := command.getSourceFiles(sourceDir)
s.Require().NoError(err)
s.ElementsMatch(files, []string{
filepath.Join(sourceDir, "test.txt"),
filepath.Join(sourceDir, "test1.txt"),
filepath.Join(sourceDir, "dir1/test.txt"),
filepath.Join(sourceDir, "dir1/dir11/test.txt"),
filepath.Join(sourceDir, "dir2/test.txt"),
})
}

func (s *VendorPublishCommandTestSuite) TestPathsForPackageOrGroup() {
tests := []struct {
name string
Expand Down Expand Up @@ -197,64 +262,71 @@ func (s *VendorPublishCommandTestSuite) TestPublish() {
command := &VendorPublishCommand{}

// Create temporary source and target directories for testing
sourceData := "test"
sourceDir, err := os.MkdirTemp("", "source")
s.Nil(err)
s.Require().Nil(err)
defer func(path string) {
err = os.RemoveAll(path)
if err != nil {
if err := file.Remove(path); err != nil {
panic(err)
}
}(sourceDir)

targetDir, err := os.MkdirTemp("", "target")
s.Nil(err)
defer func(path string) {
err = os.RemoveAll(path)
if err != nil {
panic(err)
}
}(targetDir)
s.Require().Nil(err)

// source and target are directory
sourceFile := filepath.Join(sourceDir, "test.txt")
s.Nil(os.WriteFile(sourceFile, []byte(sourceData), 0644))
targetDir = filepath.Join(targetDir, "test")
s.Require().Nil(file.Create(sourceFile, "test"))
sourceFile = filepath.Join(sourceDir, "test1.txt")
s.Require().Nil(file.Create(sourceFile, "test"))
sourceFile = filepath.Join(sourceDir, "dir1/test.txt")
s.Require().Nil(file.Create(sourceFile, "test"))
sourceFile = filepath.Join(sourceDir, "dir2/test.txt")
s.Require().Nil(file.Create(sourceFile, "test"))

// source and target are directory
result, err := command.publish(sourceDir, targetDir, false, false)
s.Nil(err)
s.Equal(1, len(result))
s.Require().Nil(err)
s.Require().Equal(4, len(result))

targetFile := filepath.Join(targetDir, "test.txt")
content, err := os.ReadFile(targetFile)
s.Nil(err)
s.Equal(sourceData, string(content))
content, err := os.ReadFile(filepath.Join(targetDir, "test.txt"))
s.Require().Nil(err)
s.Equal("test", string(content))
content, err = os.ReadFile(filepath.Join(targetDir, "test1.txt"))
s.Require().Nil(err)
s.Equal("test", string(content))
content, err = os.ReadFile(filepath.Join(targetDir, "dir1/test.txt"))
s.Require().Nil(err)
s.Equal("test", string(content))
content, err = os.ReadFile(filepath.Join(targetDir, "dir2/test.txt"))
s.Require().Nil(err)
s.Equal("test", string(content))

// source is file and target is directory
sourceFile = filepath.Join(sourceDir, "test1.txt")
s.Nil(os.WriteFile(sourceFile, []byte(sourceData), 0644))
s.Require().Nil(file.Remove(targetDir))

// source is file and target is directory
sourceFile = filepath.Join(sourceDir, "test.txt")
result, err = command.publish(sourceFile, targetDir, false, false)
s.Nil(err)
s.Equal(1, len(result))

targetFile = filepath.Join(targetDir, "test1.txt")
content, err = os.ReadFile(targetFile)
s.Nil(err)
content, err = os.ReadFile(filepath.Join(targetDir, "test.txt"))
s.Require().Nil(err)
s.Equal("test", string(content))

s.Require().Nil(file.Remove(targetDir))

// source and target are file
sourceFile = filepath.Join(sourceDir, "test2.txt")
s.Nil(os.WriteFile(sourceFile, []byte(sourceData), 0644))
targetFile = filepath.Join(targetDir, "test3.txt")
sourceFile = filepath.Join(sourceDir, "test.txt")
targetFile := filepath.Join(targetDir, "test.txt")

result, err = command.publish(sourceFile, targetFile, false, false)
s.Nil(err)
s.Equal(1, len(result))

content, err = os.ReadFile(targetFile)
s.Nil(err)
s.Require().Nil(err)
s.Equal("test", string(content))

s.Require().Nil(file.Remove(targetDir))
}

func (s *VendorPublishCommandTestSuite) TestPublishFile() {
Expand Down
2 changes: 1 addition & 1 deletion support/constant.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package support

const Version string = "v1.13.8"
const Version string = "v1.13.9"

const (
EnvRuntime = "runtime"
Expand Down

0 comments on commit d8ad014

Please sign in to comment.