github.com/khulnasoft/codebase@v0.0.0-20231214144635-a707781cbb24/service/github/githubutils/utils_test.go (about)

     1  package githubutils
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/khulnasoft/codebase/proto/rdf"
     8  )
     9  
    10  func TestLinkedMarkdownDiagnostic(t *testing.T) {
    11  	tests := []struct {
    12  		owner, repo, sha string
    13  		d                *rdf.Diagnostic
    14  		serverUrl        string
    15  		want             string
    16  	}{
    17  		{
    18  			owner: "o",
    19  			repo:  "r",
    20  			sha:   "s",
    21  			d: &rdf.Diagnostic{
    22  				Location: &rdf.Location{
    23  					Path: "path/to/file.txt",
    24  					Range: &rdf.Range{Start: &rdf.Position{
    25  						Line:   1414,
    26  						Column: 14,
    27  					}},
    28  				},
    29  				Message: "msg",
    30  			},
    31  			serverUrl: "",
    32  			want:      "[path/to/file.txt|1414 col 14|](https://github.com/o/r/blob/s/path/to/file.txt#L1414) msg",
    33  		},
    34  		{
    35  			owner: "o",
    36  			repo:  "r",
    37  			sha:   "s",
    38  			d: &rdf.Diagnostic{
    39  				Location: &rdf.Location{
    40  					Path: "path/to/file.txt",
    41  					Range: &rdf.Range{Start: &rdf.Position{
    42  						Line:   1414,
    43  						Column: 0,
    44  					}},
    45  				},
    46  				Message: "msg",
    47  			},
    48  			serverUrl: "",
    49  			want:      "[path/to/file.txt|1414|](https://github.com/o/r/blob/s/path/to/file.txt#L1414) msg",
    50  		},
    51  		{
    52  			owner: "o",
    53  			repo:  "r",
    54  			sha:   "s",
    55  			d: &rdf.Diagnostic{
    56  				Location: &rdf.Location{
    57  					Path: "path/to/file.txt",
    58  				},
    59  				Message: "msg",
    60  			},
    61  			serverUrl: "",
    62  			want:      "[path/to/file.txt||](https://github.com/o/r/blob/s/path/to/file.txt) msg",
    63  		},
    64  		{
    65  			owner: "o",
    66  			repo:  "r",
    67  			sha:   "s",
    68  			d: &rdf.Diagnostic{
    69  				Location: &rdf.Location{
    70  					Path: "path/to/file.txt",
    71  				},
    72  				Message: "msg",
    73  			},
    74  			serverUrl: "https://xpto.com",
    75  			want:      "[path/to/file.txt||](https://xpto.com/o/r/blob/s/path/to/file.txt) msg",
    76  		},
    77  	}
    78  	for _, tt := range tests {
    79  		if errUnset := os.Unsetenv("GITHUB_SERVER_URL"); errUnset != nil {
    80  			t.Error(errUnset)
    81  		}
    82  		if tt.serverUrl != "" {
    83  			if errSet := os.Setenv("GITHUB_SERVER_URL", tt.serverUrl); errSet != nil {
    84  				t.Error(errSet)
    85  			}
    86  		}
    87  		if got := LinkedMarkdownDiagnostic(tt.owner, tt.repo, tt.sha, tt.d); got != tt.want {
    88  			t.Errorf("LinkedMarkdownDiagnostic(%q, %q, %q, %#v) = %q, want %q",
    89  				tt.owner, tt.repo, tt.sha, tt.d, got, tt.want)
    90  		}
    91  	}
    92  }