Skip to content

Commit

Permalink
Create log file parent path (#80)
Browse files Browse the repository at this point in the history
Addressing: Open-CMSIS-Pack/devtools#1042

- If the path doesn't exist, create a log file path
- Throws an error, when unable to create a directory or file
- Stop the execution in case of an error
  • Loading branch information
soumeh01 authored Aug 11, 2023
1 parent 413e577 commit 32fb9a0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/cbuild/commands/build/buildcprj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ func TestPreLogConfiguration(t *testing.T) {
assert.Equal(log.DebugLevel, log.GetLevel())
})

t.Run("test invalid path to log file", func(t *testing.T) {
t.Run("test path generation to log file", func(t *testing.T) {
os.RemoveAll(logDir)

cmd := commands.NewRootCmd()
cmd.SetArgs([]string{"buildcprj", cprjFile, "--log", logFile, "-C"})
_ = cmd.Execute()
_, err := os.Stat(logFile)
assert.True(os.IsNotExist(err))
assert.False(os.IsNotExist(err))
})

t.Run("test valid path to log file", func(t *testing.T) {
Expand Down
20 changes: 12 additions & 8 deletions cmd/cbuild/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`

func preConfiguration(cmd *cobra.Command, args []string) (err error) {
func preConfiguration(cmd *cobra.Command, args []string) error {
// configure log level
log.SetLevel(log.InfoLevel)
debug, _ := cmd.Flags().GetBool("debug")
Expand All @@ -70,16 +70,20 @@ func preConfiguration(cmd *cobra.Command, args []string) (err error) {
log.SetLevel(log.ErrorLevel)
}
if logFile != "" {
logFile, err := os.Create(logFile)
parentLogDir := filepath.Dir(logFile)
if _, err := os.Stat(parentLogDir); os.IsNotExist(err) {
if err := os.MkdirAll(parentLogDir, 0755); err != nil {
return err
}
}
file, err := os.Create(logFile)
if err != nil {
log.Warn("error creating log file")
fmt.Println(err.Error())
} else {
multiWriter := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(multiWriter)
return err
}
multiWriter := io.MultiWriter(os.Stdout, file)
log.SetOutput(multiWriter)
}
return
return nil
}

func NewRootCmd() *cobra.Command {
Expand Down
43 changes: 40 additions & 3 deletions cmd/cbuild/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestPreLogConfiguration(t *testing.T) {
assert.Equal(log.DebugLevel, log.GetLevel())
})

t.Run("test invalid path to log file", func(t *testing.T) {
t.Run("test path generation to log file", func(t *testing.T) {
os.RemoveAll(logDir)

cmd := commands.NewRootCmd()
Expand All @@ -109,11 +109,48 @@ func TestPreLogConfiguration(t *testing.T) {
assert.Nil(err)

_, err = os.Stat(logFile)
assert.True(os.IsNotExist(err))
assert.False(os.IsNotExist(err))
})

t.Run("test invalid log file path error", func(t *testing.T) {
os.RemoveAll(logDir)
if _, err := os.Stat(logDir); os.IsNotExist(err) {
_ = os.MkdirAll(logDir, 0755)
}
file := logDir + "/temp"
_, err := os.Create(file)
assert.Nil(err)

invalidLogFilePath := file + "/test/logfile.log"
cmd := commands.NewRootCmd()
cmd.SetArgs([]string{"--log", invalidLogFilePath, "--version"})
err = cmd.Execute()

// Can't make subdirectory of file.
assert.Error(err)
_, err = os.Stat(invalidLogFilePath)
assert.Error(err)
})

t.Run("test log file creation error", func(t *testing.T) {
if _, err := os.Stat(logDir); os.IsNotExist(err) {
_ = os.MkdirAll(logDir, 0755)
}
invalidLogFile := logDir

cmd := commands.NewRootCmd()
cmd.SetArgs([]string{"--log", invalidLogFile, "--version"})
err := cmd.Execute()
assert.Error(err)

// log file should get generated
fileInfo, err := os.Stat(invalidLogFile)
assert.Nil(err)
assert.False(fileInfo.Mode().IsRegular())
})

t.Run("test valid path to log file", func(t *testing.T) {
_ = os.MkdirAll(logDir, 0755)
os.RemoveAll(logDir)

cmd := commands.NewRootCmd()
cmd.SetArgs([]string{"--log", logFile, "--version"})
Expand Down

0 comments on commit 32fb9a0

Please sign in to comment.