github.com/goravel/framework@v1.13.9/foundation/console/vendor_publish_command_test.go (about)

     1  package console
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/goravel/framework/support/file"
    11  )
    12  
    13  type VendorPublishCommandTestSuite struct {
    14  	suite.Suite
    15  }
    16  
    17  func TestVendorPublishCommandTestSuite(t *testing.T) {
    18  	suite.Run(t, new(VendorPublishCommandTestSuite))
    19  }
    20  
    21  func (s *VendorPublishCommandTestSuite) SetupTest() {
    22  
    23  }
    24  
    25  func (s *VendorPublishCommandTestSuite) TestGetSourceFiles() {
    26  	command := &VendorPublishCommand{}
    27  
    28  	sourceDir, err := os.MkdirTemp("", "source")
    29  	s.Require().Nil(err)
    30  	defer func(path string) {
    31  		if err := file.Remove(path); err != nil {
    32  			panic(err)
    33  		}
    34  	}(sourceDir)
    35  
    36  	sourceFile := filepath.Join(sourceDir, "test.txt")
    37  	s.Require().Nil(file.Create(sourceFile, "test"))
    38  	sourceFile = filepath.Join(sourceDir, "dir1/test.txt")
    39  	s.Require().Nil(file.Create(sourceFile, "test"))
    40  
    41  	files, err := command.getSourceFiles(filepath.Join(sourceDir, "test.txt"))
    42  	s.Require().NoError(err)
    43  	s.ElementsMatch(files, []string{
    44  		filepath.Join(sourceDir, "test.txt"),
    45  	})
    46  
    47  	files, err = command.getSourceFiles(sourceDir)
    48  	s.Require().NoError(err)
    49  	s.ElementsMatch(files, []string{
    50  		filepath.Join(sourceDir, "test.txt"),
    51  		filepath.Join(sourceDir, "dir1/test.txt"),
    52  	})
    53  }
    54  
    55  func (s *VendorPublishCommandTestSuite) TestGetSourceFilesForDir() {
    56  	command := &VendorPublishCommand{}
    57  
    58  	sourceDir, err := os.MkdirTemp("", "source")
    59  	s.Require().Nil(err)
    60  	defer func(path string) {
    61  		if err := file.Remove(path); err != nil {
    62  			panic(err)
    63  		}
    64  	}(sourceDir)
    65  
    66  	sourceFile := filepath.Join(sourceDir, "test.txt")
    67  	s.Require().Nil(file.Create(sourceFile, "test"))
    68  	sourceFile = filepath.Join(sourceDir, "test1.txt")
    69  	s.Require().Nil(file.Create(sourceFile, "test"))
    70  	sourceFile = filepath.Join(sourceDir, "dir1/test.txt")
    71  	s.Require().Nil(file.Create(sourceFile, "test"))
    72  	sourceFile = filepath.Join(sourceDir, "dir1/dir11/test.txt")
    73  	s.Require().Nil(file.Create(sourceFile, "test"))
    74  	sourceFile = filepath.Join(sourceDir, "dir2/test.txt")
    75  	s.Require().Nil(file.Create(sourceFile, "test"))
    76  
    77  	files, err := command.getSourceFiles(sourceDir)
    78  	s.Require().NoError(err)
    79  	s.ElementsMatch(files, []string{
    80  		filepath.Join(sourceDir, "test.txt"),
    81  		filepath.Join(sourceDir, "test1.txt"),
    82  		filepath.Join(sourceDir, "dir1/test.txt"),
    83  		filepath.Join(sourceDir, "dir1/dir11/test.txt"),
    84  		filepath.Join(sourceDir, "dir2/test.txt"),
    85  	})
    86  }
    87  
    88  func (s *VendorPublishCommandTestSuite) TestPathsForPackageOrGroup() {
    89  	tests := []struct {
    90  		name          string
    91  		packageName   string
    92  		group         string
    93  		publishes     map[string]map[string]string
    94  		publishGroups map[string]map[string]string
    95  		expectPaths   map[string]string
    96  	}{
    97  		{
    98  			name: "packageName and group are empty",
    99  		},
   100  		{
   101  			name:        "packageName and group are not empty, and have same path",
   102  			packageName: "github.com/goravel/sms",
   103  			group:       "public",
   104  			publishes: map[string]map[string]string{
   105  				"github.com/goravel/sms": {
   106  					"config.go": "config.go",
   107  				},
   108  			},
   109  			publishGroups: map[string]map[string]string{
   110  				"public": {
   111  					"config.go": "config.go",
   112  				},
   113  			},
   114  			expectPaths: map[string]string{
   115  				"config.go": "config.go",
   116  			},
   117  		},
   118  		{
   119  			name:        "packageName and group are not empty, but doesn't have same path",
   120  			packageName: "github.com/goravel/sms",
   121  			group:       "public",
   122  			publishes: map[string]map[string]string{
   123  				"github.com/goravel/sms": {
   124  					"config.go": "config.go",
   125  				},
   126  			},
   127  			publishGroups: map[string]map[string]string{
   128  				"public": {
   129  					"config1.go": "config.go",
   130  				},
   131  			},
   132  			expectPaths: map[string]string{},
   133  		},
   134  		{
   135  			name:  "packageName is empty, group is not empty",
   136  			group: "public",
   137  			publishes: map[string]map[string]string{
   138  				"github.com/goravel/sms": {
   139  					"config.go": "config.go",
   140  				},
   141  			},
   142  			publishGroups: map[string]map[string]string{
   143  				"public": {
   144  					"config1.go": "config.go",
   145  				},
   146  			},
   147  			expectPaths: map[string]string{
   148  				"config1.go": "config.go",
   149  			},
   150  		},
   151  		{
   152  			name:        "packageName is not empty, group is empty",
   153  			packageName: "github.com/goravel/sms",
   154  			publishes: map[string]map[string]string{
   155  				"github.com/goravel/sms": {
   156  					"config.go": "config.go",
   157  				},
   158  			},
   159  			publishGroups: map[string]map[string]string{
   160  				"public": {
   161  					"config1.go": "config.go",
   162  				},
   163  			},
   164  			expectPaths: map[string]string{
   165  				"config.go": "config.go",
   166  			},
   167  		},
   168  	}
   169  
   170  	for _, test := range tests {
   171  		s.Run(test.name, func() {
   172  			command := NewVendorPublishCommand(test.publishes, test.publishGroups)
   173  			s.Equal(test.expectPaths, command.pathsForPackageOrGroup(test.packageName, test.group))
   174  		})
   175  	}
   176  }
   177  
   178  func (s *VendorPublishCommandTestSuite) TestPathsForProviderAndGroup() {
   179  	tests := []struct {
   180  		name          string
   181  		packageName   string
   182  		group         string
   183  		publishes     map[string]map[string]string
   184  		publishGroups map[string]map[string]string
   185  		expectPaths   map[string]string
   186  	}{
   187  		{
   188  			name:        "not found packageName",
   189  			packageName: "github.com/goravel/sms1",
   190  			group:       "public",
   191  			publishes: map[string]map[string]string{
   192  				"github.com/goravel/sms": {
   193  					"config.go": "config.go",
   194  				},
   195  			},
   196  			publishGroups: map[string]map[string]string{
   197  				"public": {
   198  					"config1.go": "config.go",
   199  				},
   200  			},
   201  		},
   202  		{
   203  			name:        "not found group",
   204  			packageName: "github.com/goravel/sms",
   205  			group:       "public1",
   206  			publishes: map[string]map[string]string{
   207  				"github.com/goravel/sms": {
   208  					"config.go": "config.go",
   209  				},
   210  			},
   211  			publishGroups: map[string]map[string]string{
   212  				"public": {
   213  					"config1.go": "config.go",
   214  				},
   215  			},
   216  		},
   217  		{
   218  			name:        "does not have Intersection",
   219  			packageName: "github.com/goravel/sms",
   220  			group:       "public",
   221  			publishes: map[string]map[string]string{
   222  				"github.com/goravel/sms": {
   223  					"config.go": "config.go",
   224  				},
   225  			},
   226  			publishGroups: map[string]map[string]string{
   227  				"public": {
   228  					"config1.go": "config.go",
   229  				},
   230  			},
   231  			expectPaths: map[string]string{},
   232  		},
   233  		{
   234  			name:        "have Intersection",
   235  			packageName: "github.com/goravel/sms",
   236  			group:       "public",
   237  			publishes: map[string]map[string]string{
   238  				"github.com/goravel/sms": {
   239  					"config.go": "config.go",
   240  				},
   241  			},
   242  			publishGroups: map[string]map[string]string{
   243  				"public": {
   244  					"config.go": "config.go",
   245  				},
   246  			},
   247  			expectPaths: map[string]string{
   248  				"config.go": "config.go",
   249  			},
   250  		},
   251  	}
   252  
   253  	for _, test := range tests {
   254  		s.Run(test.name, func() {
   255  			command := NewVendorPublishCommand(test.publishes, test.publishGroups)
   256  			s.Equal(test.expectPaths, command.pathsForProviderAndGroup(test.packageName, test.group))
   257  		})
   258  	}
   259  }
   260  
   261  func (s *VendorPublishCommandTestSuite) TestPublish() {
   262  	command := &VendorPublishCommand{}
   263  
   264  	// Create temporary source and target directories for testing
   265  	sourceDir, err := os.MkdirTemp("", "source")
   266  	s.Require().Nil(err)
   267  	defer func(path string) {
   268  		if err := file.Remove(path); err != nil {
   269  			panic(err)
   270  		}
   271  	}(sourceDir)
   272  
   273  	targetDir, err := os.MkdirTemp("", "target")
   274  	s.Require().Nil(err)
   275  
   276  	sourceFile := filepath.Join(sourceDir, "test.txt")
   277  	s.Require().Nil(file.Create(sourceFile, "test"))
   278  	sourceFile = filepath.Join(sourceDir, "test1.txt")
   279  	s.Require().Nil(file.Create(sourceFile, "test"))
   280  	sourceFile = filepath.Join(sourceDir, "dir1/test.txt")
   281  	s.Require().Nil(file.Create(sourceFile, "test"))
   282  	sourceFile = filepath.Join(sourceDir, "dir2/test.txt")
   283  	s.Require().Nil(file.Create(sourceFile, "test"))
   284  
   285  	// source and target are directory
   286  	result, err := command.publish(sourceDir, targetDir, false, false)
   287  	s.Require().Nil(err)
   288  	s.Require().Equal(4, len(result))
   289  
   290  	content, err := os.ReadFile(filepath.Join(targetDir, "test.txt"))
   291  	s.Require().Nil(err)
   292  	s.Equal("test", string(content))
   293  	content, err = os.ReadFile(filepath.Join(targetDir, "test1.txt"))
   294  	s.Require().Nil(err)
   295  	s.Equal("test", string(content))
   296  	content, err = os.ReadFile(filepath.Join(targetDir, "dir1/test.txt"))
   297  	s.Require().Nil(err)
   298  	s.Equal("test", string(content))
   299  	content, err = os.ReadFile(filepath.Join(targetDir, "dir2/test.txt"))
   300  	s.Require().Nil(err)
   301  	s.Equal("test", string(content))
   302  
   303  	s.Require().Nil(file.Remove(targetDir))
   304  
   305  	// source is file and target is directory
   306  	sourceFile = filepath.Join(sourceDir, "test.txt")
   307  	result, err = command.publish(sourceFile, targetDir, false, false)
   308  	s.Nil(err)
   309  	s.Equal(1, len(result))
   310  
   311  	content, err = os.ReadFile(filepath.Join(targetDir, "test.txt"))
   312  	s.Require().Nil(err)
   313  	s.Equal("test", string(content))
   314  
   315  	s.Require().Nil(file.Remove(targetDir))
   316  
   317  	// source and target are file
   318  	sourceFile = filepath.Join(sourceDir, "test.txt")
   319  	targetFile := filepath.Join(targetDir, "test.txt")
   320  
   321  	result, err = command.publish(sourceFile, targetFile, false, false)
   322  	s.Nil(err)
   323  	s.Equal(1, len(result))
   324  
   325  	content, err = os.ReadFile(targetFile)
   326  	s.Require().Nil(err)
   327  	s.Equal("test", string(content))
   328  
   329  	s.Require().Nil(file.Remove(targetDir))
   330  }
   331  
   332  func (s *VendorPublishCommandTestSuite) TestPublishFile() {
   333  	command := &VendorPublishCommand{}
   334  
   335  	sourceData := "This is a test file."
   336  	sourceFile := "./test_source.txt"
   337  	targetFile := "./test_target.txt"
   338  
   339  	// Create a test source file
   340  	err := os.WriteFile(sourceFile, []byte(sourceData), 0644)
   341  	s.Nil(err)
   342  
   343  	// Ensure publishFile creates target file when it doesn't exist and 'existing' flag is set
   344  	created, err := command.publishFile(sourceFile, targetFile, true, false)
   345  	s.Nil(err)
   346  	s.False(created)
   347  
   348  	// Ensure publishFile returns false when target file already exists and 'force' flag is not set
   349  	created, err = command.publishFile(sourceFile, targetFile, false, false)
   350  	s.Nil(err)
   351  	s.True(created)
   352  	content, err := os.ReadFile(targetFile)
   353  	s.Nil(err)
   354  	s.Equal(string(content), sourceData)
   355  
   356  	created, err = command.publishFile(sourceFile, targetFile, false, false)
   357  	s.Nil(err)
   358  	s.False(created)
   359  
   360  	// Ensure publishFile overwrites target file when 'force' flag is set
   361  	newSourceData := "This is a new test file."
   362  	err = os.WriteFile(sourceFile, []byte(newSourceData), 0644)
   363  	s.Nil(err)
   364  
   365  	created, err = command.publishFile(sourceFile, targetFile, false, true)
   366  	s.Nil(err)
   367  	s.True(created)
   368  	content, err = os.ReadFile(targetFile)
   369  	s.Nil(err)
   370  	s.Equal(string(content), newSourceData)
   371  
   372  	// Clean up test files
   373  	s.Nil(os.Remove(sourceFile))
   374  	s.Nil(os.Remove(targetFile))
   375  }