Files
atlantis/testing/temp_files.go
PePe Amengual 7567b31ccd fix: resolve linter issues to enable golangci-lint update
- Fix all errcheck issues (unhandled errors for Close(), Fprintln, etc.)
- Fix gosec issues with appropriate nolint comments for test code
- Fix misspell issues and add nolint for GitLab API field names
- Fix revive issues (dot-imports in tests, missing package comments)
- Fix staticcheck and testifylint issues
- Add package comments where required by linter

This enables updating golangci-lint-action from v6 to v8 in the workflow.
2025-06-27 11:12:19 -07:00

59 lines
1.8 KiB
Go

package testing
import (
"os"
"path/filepath"
"testing"
)
// DirStructure creates a directory structure in a temporary directory.
// structure describes the dir structure. If the value is another map, then the
// key is the name of a directory. If the value is nil, then the key is the name
// of a file. If val is a string then key is a file name and val is the file's content.
// It returns the path to the temp directory containing the defined
// structure.
// Example usage:
//
// versionConfig := `
// terraform {
// required_version = "= 0.12.8"
// }
// `
// tmpDir := DirStructure(t, map[string]interface{}{
// "pulldir": map[string]interface{}{
// "project1": map[string]interface{}{
// "main.tf": nil,
// },
// "project2": map[string]interface{}{,
// "main.tf": versionConfig,
// },
// },
// })
func DirStructure(t *testing.T, structure map[string]interface{}) string {
tmpDir := t.TempDir()
dirStructureGo(t, tmpDir, structure)
return tmpDir
}
func dirStructureGo(t *testing.T, parentDir string, structure map[string]interface{}) {
for key, val := range structure {
// If val is nil then key is a filename and we just create it
if val == nil {
_, err := os.Create(filepath.Join(parentDir, key)) // nolint: gosec
Ok(t, err)
continue
}
// If val is another map then key is a dir
if dirContents, ok := val.(map[string]interface{}); ok {
subDir := filepath.Join(parentDir, key)
Ok(t, os.Mkdir(subDir, 0700))
// Recurse and create contents.
dirStructureGo(t, subDir, dirContents)
} else if fileContent, ok := val.(string); ok {
// If val is a string then key is a file name and val is the file's content
err := os.WriteFile(filepath.Join(parentDir, key), []byte(fileContent), 0600)
Ok(t, err)
}
}
}