Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create necessary parent dirs when making work dir #10

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions buildmodel/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func BindPRFlags() *PRFlags {
// submits the resulting commit as a GitHub PR, approves with a second account, and enables the
// GitHub auto-merge feature.
func SubmitUpdatePR(f *PRFlags) error {
gitDir, err := GetWorkPathInDir(*f.tempGitDir)
gitDir, err := MakeWorkDir(*f.tempGitDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -317,12 +317,15 @@ func SubmitUpdatePR(f *PRFlags) error {
return nil
}

// GetWorkPathInDir creates a unique path inside the given root dir to use as a workspace. The name
// MakeWorkDir creates a unique path inside the given root dir to use as a workspace. The name
// starts with the local time in a sortable format to help with browsing multiple workspaces. This
// function allows a command to run multiple times in sequence without overwriting or deleting the
// old data, for diagnostic purposes.
func GetWorkPathInDir(rootDir string) (string, error) {
// old data, for diagnostic purposes. This function uses os.MkdirAll to ensure the root dir exists.
func MakeWorkDir(rootDir string) (string, error) {
pathDate := time.Now().Format("2006-01-02_15-04-05")
if err := os.MkdirAll(rootDir, os.ModePerm); err != nil {
return "", err
}
return os.MkdirTemp(rootDir, fmt.Sprintf("%s_*", pathDate))
}

Expand Down
28 changes: 28 additions & 0 deletions buildmodel/commands_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

package buildmodel

import (
"path"
"testing"
)

func TestMakeWorkDir(t *testing.T) {
tests := []struct {
name string
rootDir string
}{
{"InsideExistingDir", t.TempDir()},
{"InsideNonexistentDir", path.Join(t.TempDir(), "nonexistent")},
{"DeeplyInsideNonexistentDir", path.Join(t.TempDir(), "nonexistent", "a", "b", "c")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := MakeWorkDir(tt.rootDir)
if err != nil {
t.Error(err)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func main() {
fmt.Printf("No entries found in config file: %v\n", *syncConfig)
}

currentRunGitDir, err := buildmodel.GetWorkPathInDir(*tempGitDir)
currentRunGitDir, err := buildmodel.MakeWorkDir(*tempGitDir)
if err != nil {
log.Panic(err)
}
Expand Down