absmach / magistrala

Industrial IoT Messaging and Device Management Platform
https://www.abstractmachines.fr/magistrala.html
Apache License 2.0
2.46k stars 671 forks source link

MG-887 - Add Users CLI Tests. #2250

Closed WashingtonKK closed 2 months ago

WashingtonKK commented 4 months ago

What type of PR is this?

This is an enhancement because it adds cli tests for users.

What does this do?

This adds tests for cli, to check for invalid args input, and logging of output.

Which issue(s) does this PR fix/relate to?

Have you included tests for your changes?

Yes, I have included tests for my changes.

Did you document any new/modified feature?

No, I have not updated the documentation because there are no new features, just an increase in scope of the existing tests to cover CLI.

Notes

This is the first PR in this series, to be merged first.

WashingtonKK commented 3 months ago

At logs, we directly printing to os.stdout and os.stderr, Actually, cobra should take care to redirecting to out or error

So We need to modify the log to like below

func logError( cmd *cobra.Command , err error) {
  boldRed := color.New(color.FgRed, color.Bold)
  boldRed.Fprintf(cmd.OutOrStderr(, "\nerror: ")

  fmt.Fprintf(cmd.OutOrStderr(), "%s\n\n", color.RedString(err.Error()))
}

Please modify the log functions like above and test

if it works , then create a custom cobra.Command, like below

type cmd struct {
  *cobra.Command
}

func (c *cmd) logJSON(iList ...interface{}) {
  for _, i := range iList {
      m, err := json.Marshal(i)
      if err != nil {
          c.logError(err)
          return
      }

      pj, err := prettyjson.Format(m)
      if err != nil {
          c.logError(err)
          return
      }

      fmt.Fprintf(c.OutOrStdout(), "\n%s\n\n", string(pj))
  }
}

func (c *cmd) logUsage(u string) {
  fmt.Fprintf(c.OutOrStdout(), color.YellowString("\nusage: %s\n\n"), u)
}

func (c *cmd) logError(err error) {
  boldRed := color.New(color.FgRed, color.Bold)
  boldRed.Fprintf(c.OutOrStderr(), "\nerror: ")

  fmt.Fprintf(c.OutOrStderr(), "%s\n\n", color.RedString(err.Error()))
}

@arvindh123 , this implementation does not work, and coverage is still not generated.

WashingtonKK commented 3 months ago

At logs, we directly printing to os.stdout and os.stderr, Actually, cobra should take care to redirecting to out or error So We need to modify the log to like below

func logError( cmd *cobra.Command , err error) {
    boldRed := color.New(color.FgRed, color.Bold)
    boldRed.Fprintf(cmd.OutOrStderr(, "\nerror: ")

    fmt.Fprintf(cmd.OutOrStderr(), "%s\n\n", color.RedString(err.Error()))
}

Please modify the log functions like above and test if it works , then create a custom cobra.Command, like below

type cmd struct {
    *cobra.Command
}

func (c *cmd) logJSON(iList ...interface{}) {
    for _, i := range iList {
        m, err := json.Marshal(i)
        if err != nil {
            c.logError(err)
            return
        }

        pj, err := prettyjson.Format(m)
        if err != nil {
            c.logError(err)
            return
        }

        fmt.Fprintf(c.OutOrStdout(), "\n%s\n\n", string(pj))
    }
}

func (c *cmd) logUsage(u string) {
    fmt.Fprintf(c.OutOrStdout(), color.YellowString("\nusage: %s\n\n"), u)
}

func (c *cmd) logError(err error) {
    boldRed := color.New(color.FgRed, color.Bold)
    boldRed.Fprintf(c.OutOrStderr(), "\nerror: ")

    fmt.Fprintf(c.OutOrStderr(), "%s\n\n", color.RedString(err.Error()))
}

@arvindh123 , this implementation does not work, and coverage is still not generated.

I suppose this is because the function OutOrStdout simply returns the writer that is set using SetOut and SerErr and uses it to write all output which still does not solve our issue.

dborovcanin commented 2 months ago

Closing as duplicate of https://github.com/absmach/magistrala/pull/2319.