SetCoveragePath takes a goCovPath and sets the coverage path accordingly. It returns an error if the path is a file or if the path does not exist.
(logger *zap.Logger, goCovPath string)
| 776 | // SetCoveragePath takes a goCovPath and sets the coverage path accordingly. |
| 777 | // It returns an error if the path is a file or if the path does not exist. |
| 778 | func SetCoveragePath(logger *zap.Logger, goCovPath string) (string, error) { |
| 779 | if goCovPath == "" { |
| 780 | // Calculate the current path and create a coverage-reports directory |
| 781 | currentPath, err := GetAbsPath("") |
| 782 | if err != nil { |
| 783 | LogError(logger, err, "failed to get the current working directory") |
| 784 | return "", err |
| 785 | } |
| 786 | goCovPath = currentPath + "/coverage-reports" |
| 787 | if err := makeDirectory(goCovPath); err != nil { |
| 788 | LogError(logger, err, "failed to create coverage-reports directory", zap.String("CoverageReportPath", goCovPath)) |
| 789 | return "", err |
| 790 | } |
| 791 | return goCovPath, nil |
| 792 | } |
| 793 | |
| 794 | goCovPath, err := GetAbsPath(goCovPath) |
| 795 | if err != nil { |
| 796 | LogError(logger, err, "failed to get the absolute path for the coverage report path", zap.String("CoverageReportPath", goCovPath)) |
| 797 | return "", err |
| 798 | } |
| 799 | // Check if the path is a directory |
| 800 | dirInfo, err := os.Stat(goCovPath) |
| 801 | if err != nil { |
| 802 | if os.IsNotExist(err) { |
| 803 | LogError(logger, err, "the provided path does not exist", zap.String("CoverageReportPath", goCovPath)) |
| 804 | return "", err |
| 805 | } |
| 806 | LogError(logger, err, "failed to check the coverage report path", zap.String("CoverageReportPath", goCovPath)) |
| 807 | return "", err |
| 808 | } |
| 809 | if !dirInfo.IsDir() { |
| 810 | msg := "the coverage report path is not a directory. Please provide a valid path to a directory for go coverage reports" |
| 811 | |
| 812 | LogError(logger, nil, msg, zap.String("CoverageReportPath", goCovPath)) |
| 813 | return "", errors.New("the path provided is not a directory") |
| 814 | } |
| 815 | |
| 816 | return goCovPath, nil |
| 817 | } |
| 818 | |
| 819 | type ErrType string |
| 820 |
no test coverage detected