feat(bootstrap): add automatic environment setup with bootstrap scripts

Implement bootstrap functionality for streamlined dotfiles setup:
- Add 'bootstrap' command to run setup scripts manually
- Auto-execute bootstrap on 'init' with remote (--no-bootstrap to skip)
- Update README with bootstrap usage and examples
- Extend tests to cover bootstrap scenarios
This commit is contained in:
Yar Kravtsov
2025-06-03 08:33:59 +03:00
parent 1e2c9704f3
commit ae9cc175ce
7 changed files with 441 additions and 15 deletions

View File

@@ -1,6 +1,7 @@
package core
import (
"fmt"
"os"
"path/filepath"
"strings"
@@ -749,6 +750,76 @@ func (suite *CoreTestSuite) TestMultihostIsolation() {
suite.Equal(hostContent, string(symlinkContent))
}
// Test bootstrap script detection
func (suite *CoreTestSuite) TestFindBootstrapScript() {
err := suite.lnk.Init()
suite.Require().NoError(err)
// Test with no bootstrap script
scriptPath, err := suite.lnk.FindBootstrapScript()
suite.NoError(err)
suite.Empty(scriptPath)
// Test with bootstrap.sh
bootstrapScript := filepath.Join(suite.tempDir, "lnk", "bootstrap.sh")
err = os.WriteFile(bootstrapScript, []byte("#!/bin/bash\necho 'test'"), 0644)
suite.Require().NoError(err)
scriptPath, err = suite.lnk.FindBootstrapScript()
suite.NoError(err)
suite.Equal("bootstrap.sh", scriptPath)
}
// Test bootstrap script execution
func (suite *CoreTestSuite) TestRunBootstrapScript() {
err := suite.lnk.Init()
suite.Require().NoError(err)
// Create a test script that creates a marker file
bootstrapScript := filepath.Join(suite.tempDir, "lnk", "test.sh")
markerFile := filepath.Join(suite.tempDir, "lnk", "bootstrap-executed.txt")
scriptContent := fmt.Sprintf("#!/bin/bash\ntouch %s\necho 'Bootstrap executed'", markerFile)
err = os.WriteFile(bootstrapScript, []byte(scriptContent), 0755)
suite.Require().NoError(err)
// Run the bootstrap script
err = suite.lnk.RunBootstrapScript("test.sh")
suite.NoError(err)
// Verify the marker file was created
suite.FileExists(markerFile)
}
// Test bootstrap script execution with error
func (suite *CoreTestSuite) TestRunBootstrapScriptWithError() {
err := suite.lnk.Init()
suite.Require().NoError(err)
// Create a script that will fail
bootstrapScript := filepath.Join(suite.tempDir, "lnk", "failing.sh")
scriptContent := "#!/bin/bash\nexit 1"
err = os.WriteFile(bootstrapScript, []byte(scriptContent), 0755)
suite.Require().NoError(err)
// Run the bootstrap script - should fail
err = suite.lnk.RunBootstrapScript("failing.sh")
suite.Error(err)
suite.Contains(err.Error(), "Bootstrap script failed")
}
// Test running bootstrap on non-existent script
func (suite *CoreTestSuite) TestRunBootstrapScriptNotFound() {
err := suite.lnk.Init()
suite.Require().NoError(err)
// Try to run non-existent script
err = suite.lnk.RunBootstrapScript("nonexistent.sh")
suite.Error(err)
suite.Contains(err.Error(), "Bootstrap script not found")
}
func TestCoreSuite(t *testing.T) {
suite.Run(t, new(CoreTestSuite))
}