github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/internal/pipe/milestone/milestone_test.go (about)

     1  package milestone
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/goreleaser/goreleaser/internal/artifact"
     9  	"github.com/goreleaser/goreleaser/internal/client"
    10  	"github.com/goreleaser/goreleaser/internal/testlib"
    11  	"github.com/goreleaser/goreleaser/pkg/config"
    12  	"github.com/goreleaser/goreleaser/pkg/context"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestDefaultWithRepoConfig(t *testing.T) {
    17  	testlib.Mktmp(t)
    18  	testlib.GitInit(t)
    19  	testlib.GitRemoteAdd(t, "git@github.com:githubowner/githubrepo.git")
    20  
    21  	ctx := &context.Context{
    22  		Config: config.Project{
    23  			Milestones: []config.Milestone{
    24  				{
    25  					Repo: config.Repo{
    26  						Name:  "configrepo",
    27  						Owner: "configowner",
    28  					},
    29  				},
    30  			},
    31  		},
    32  	}
    33  	ctx.TokenType = context.TokenTypeGitHub
    34  	require.NoError(t, Pipe{}.Default(ctx))
    35  	require.Equal(t, "configrepo", ctx.Config.Milestones[0].Repo.Name)
    36  	require.Equal(t, "configowner", ctx.Config.Milestones[0].Repo.Owner)
    37  }
    38  
    39  func TestDefaultWithRepoRemote(t *testing.T) {
    40  	testlib.Mktmp(t)
    41  	testlib.GitInit(t)
    42  	testlib.GitRemoteAdd(t, "git@github.com:githubowner/githubrepo.git")
    43  
    44  	ctx := context.New(config.Project{
    45  		Milestones: []config.Milestone{{}},
    46  	})
    47  	ctx.TokenType = context.TokenTypeGitHub
    48  	require.NoError(t, Pipe{}.Default(ctx))
    49  	require.Equal(t, "githubrepo", ctx.Config.Milestones[0].Repo.Name)
    50  	require.Equal(t, "githubowner", ctx.Config.Milestones[0].Repo.Owner)
    51  }
    52  
    53  func TestDefaultWithNameTemplate(t *testing.T) {
    54  	ctx := &context.Context{
    55  		Config: config.Project{
    56  			Milestones: []config.Milestone{
    57  				{
    58  					NameTemplate: "confignametemplate",
    59  				},
    60  			},
    61  		},
    62  	}
    63  	require.NoError(t, Pipe{}.Default(ctx))
    64  	require.Equal(t, "confignametemplate", ctx.Config.Milestones[0].NameTemplate)
    65  }
    66  
    67  func TestDefaultWithoutGitRepo(t *testing.T) {
    68  	testlib.Mktmp(t)
    69  	ctx := &context.Context{
    70  		Config: config.Project{
    71  			Milestones: []config.Milestone{{}},
    72  		},
    73  	}
    74  	ctx.TokenType = context.TokenTypeGitHub
    75  	require.EqualError(t, Pipe{}.Default(ctx), "current folder is not a git repository")
    76  	require.Empty(t, ctx.Config.Milestones[0].Repo.String())
    77  }
    78  
    79  func TestDefaultWithoutGitRepoOrigin(t *testing.T) {
    80  	testlib.Mktmp(t)
    81  	ctx := &context.Context{
    82  		Config: config.Project{
    83  			Milestones: []config.Milestone{{}},
    84  		},
    85  	}
    86  	ctx.TokenType = context.TokenTypeGitHub
    87  	testlib.GitInit(t)
    88  	require.EqualError(t, Pipe{}.Default(ctx), "no remote configured to list refs from")
    89  	require.Empty(t, ctx.Config.Milestones[0].Repo.String())
    90  }
    91  
    92  func TestDefaultWithoutGitRepoSnapshot(t *testing.T) {
    93  	testlib.Mktmp(t)
    94  	ctx := &context.Context{
    95  		Config: config.Project{
    96  			Milestones: []config.Milestone{{}},
    97  		},
    98  	}
    99  	ctx.TokenType = context.TokenTypeGitHub
   100  	ctx.Snapshot = true
   101  	require.NoError(t, Pipe{}.Default(ctx))
   102  	require.Empty(t, ctx.Config.Milestones[0].Repo.String())
   103  }
   104  
   105  func TestDefaultWithoutNameTemplate(t *testing.T) {
   106  	ctx := &context.Context{
   107  		Config: config.Project{
   108  			Milestones: []config.Milestone{{}},
   109  		},
   110  	}
   111  	require.NoError(t, Pipe{}.Default(ctx))
   112  	require.Equal(t, "{{ .Tag }}", ctx.Config.Milestones[0].NameTemplate)
   113  }
   114  
   115  func TestString(t *testing.T) {
   116  	require.NotEmpty(t, Pipe{}.String())
   117  }
   118  
   119  func TestPublishCloseDisabled(t *testing.T) {
   120  	ctx := context.New(config.Project{
   121  		Milestones: []config.Milestone{
   122  			{
   123  				Close: false,
   124  			},
   125  		},
   126  	})
   127  	client := &DummyClient{}
   128  	testlib.AssertSkipped(t, doPublish(ctx, client))
   129  	require.Equal(t, "", client.ClosedMilestone)
   130  }
   131  
   132  func TestPublishCloseEnabled(t *testing.T) {
   133  	ctx := context.New(config.Project{
   134  		Milestones: []config.Milestone{
   135  			{
   136  				Close:        true,
   137  				NameTemplate: defaultNameTemplate,
   138  				Repo: config.Repo{
   139  					Name:  "configrepo",
   140  					Owner: "configowner",
   141  				},
   142  			},
   143  		},
   144  	})
   145  	ctx.Git.CurrentTag = "v1.0.0"
   146  	client := &DummyClient{}
   147  	require.NoError(t, doPublish(ctx, client))
   148  	require.Equal(t, "v1.0.0", client.ClosedMilestone)
   149  }
   150  
   151  func TestPublishCloseError(t *testing.T) {
   152  	config := config.Project{
   153  		Milestones: []config.Milestone{
   154  			{
   155  				Close:        true,
   156  				NameTemplate: defaultNameTemplate,
   157  				Repo: config.Repo{
   158  					Name:  "configrepo",
   159  					Owner: "configowner",
   160  				},
   161  			},
   162  		},
   163  	}
   164  	ctx := context.New(config)
   165  	ctx.Git.CurrentTag = "v1.0.0"
   166  	client := &DummyClient{
   167  		FailToCloseMilestone: true,
   168  	}
   169  	require.NoError(t, doPublish(ctx, client))
   170  	require.Equal(t, "", client.ClosedMilestone)
   171  }
   172  
   173  func TestPublishCloseFailOnError(t *testing.T) {
   174  	config := config.Project{
   175  		Milestones: []config.Milestone{
   176  			{
   177  				Close:        true,
   178  				FailOnError:  true,
   179  				NameTemplate: defaultNameTemplate,
   180  				Repo: config.Repo{
   181  					Name:  "configrepo",
   182  					Owner: "configowner",
   183  				},
   184  			},
   185  		},
   186  	}
   187  	ctx := context.New(config)
   188  	ctx.Git.CurrentTag = "v1.0.0"
   189  	client := &DummyClient{
   190  		FailToCloseMilestone: true,
   191  	}
   192  	require.Error(t, doPublish(ctx, client))
   193  	require.Equal(t, "", client.ClosedMilestone)
   194  }
   195  
   196  func TestSkip(t *testing.T) {
   197  	t.Run("skip", func(t *testing.T) {
   198  		require.True(t, Pipe{}.Skip(context.New(config.Project{})))
   199  	})
   200  
   201  	t.Run("dont skip", func(t *testing.T) {
   202  		ctx := context.New(config.Project{
   203  			Milestones: []config.Milestone{
   204  				{},
   205  			},
   206  		})
   207  		require.False(t, Pipe{}.Skip(ctx))
   208  	})
   209  }
   210  
   211  type DummyClient struct {
   212  	ClosedMilestone      string
   213  	FailToCloseMilestone bool
   214  }
   215  
   216  func (c *DummyClient) CloseMilestone(ctx *context.Context, repo client.Repo, title string) error {
   217  	if c.FailToCloseMilestone {
   218  		return errors.New("milestone failed")
   219  	}
   220  
   221  	c.ClosedMilestone = title
   222  
   223  	return nil
   224  }
   225  
   226  func (c *DummyClient) CreateRelease(ctx *context.Context, body string) (string, error) {
   227  	return "", nil
   228  }
   229  
   230  func (c *DummyClient) ReleaseURLTemplate(ctx *context.Context) (string, error) {
   231  	return "", nil
   232  }
   233  
   234  func (c *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo client.Repo, content []byte, path, msg string) error {
   235  	return nil
   236  }
   237  
   238  func (c *DummyClient) Upload(ctx *context.Context, releaseID string, artifact *artifact.Artifact, file *os.File) error {
   239  	return nil
   240  }