github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/gits/features/disable_features_test.go (about)

     1  // +build unit
     2  
     3  package features
     4  
     5  import (
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/olli-ai/jx/v2/pkg/issues"
    10  	"github.com/olli-ai/jx/v2/pkg/tests"
    11  	"github.com/olli-ai/jx/v2/pkg/util"
    12  
    13  	"github.com/olli-ai/jx/v2/pkg/gits"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestDisableIssuesForOrg(t *testing.T) {
    18  	type repo struct {
    19  		gits.GitRepository
    20  		issues      map[int]*gits.FakeIssue
    21  		projects    []gits.GitProject
    22  		wikiEnabled bool
    23  	}
    24  	type args struct {
    25  		repos    []repo
    26  		dryRun   bool
    27  		includes []string
    28  		excludes []string
    29  	}
    30  
    31  	ts := []struct {
    32  		name        string
    33  		args        args
    34  		wantErr     bool
    35  		wantIssues  bool
    36  		wantProject bool
    37  		wantWiki    bool
    38  	}{
    39  		{
    40  			name: "closeIssues",
    41  			args: args{
    42  				repos: []repo{
    43  					{
    44  						GitRepository: gits.GitRepository{
    45  							Name:         "roadrunner",
    46  							Organisation: "acme",
    47  							HasIssues:    true,
    48  							HasWiki:      false,
    49  							HasProjects:  false,
    50  						},
    51  					},
    52  				},
    53  				dryRun:   false,
    54  				includes: nil,
    55  				excludes: nil,
    56  			},
    57  			wantErr:     false,
    58  			wantIssues:  false,
    59  			wantProject: false,
    60  			wantWiki:    false,
    61  		},
    62  		{
    63  			name: "closeProjects",
    64  			args: args{
    65  				repos: []repo{
    66  					{
    67  						GitRepository: gits.GitRepository{
    68  							Name:         "roadrunner",
    69  							Organisation: "acme",
    70  							HasIssues:    false,
    71  							HasWiki:      false,
    72  							HasProjects:  true,
    73  						},
    74  					},
    75  				},
    76  				dryRun:   false,
    77  				includes: nil,
    78  				excludes: nil,
    79  			},
    80  			wantErr:     false,
    81  			wantIssues:  false,
    82  			wantProject: false,
    83  			wantWiki:    false,
    84  		},
    85  		{
    86  			name: "closeWiki",
    87  			args: args{
    88  				repos: []repo{
    89  					{
    90  						GitRepository: gits.GitRepository{
    91  
    92  							Name:         "roadrunner",
    93  							Organisation: "acme",
    94  							HasIssues:    false,
    95  							HasWiki:      true,
    96  							HasProjects:  false,
    97  						},
    98  					},
    99  				},
   100  				dryRun:   false,
   101  				includes: nil,
   102  				excludes: nil,
   103  			},
   104  			wantErr:     false,
   105  			wantIssues:  false,
   106  			wantProject: false,
   107  			wantWiki:    false,
   108  		},
   109  		{
   110  			name: "keepIssuesOpen",
   111  			args: args{
   112  				repos: []repo{
   113  					{
   114  						GitRepository: gits.GitRepository{
   115  							Name:         "roadrunner",
   116  							Organisation: "acme",
   117  							HasIssues:    true,
   118  							HasWiki:      false,
   119  							HasProjects:  false,
   120  						},
   121  						issues: map[int]*gits.FakeIssue{
   122  							1: {
   123  								Issue: &gits.GitIssue{
   124  									Title: "First issue",
   125  									State: &issues.IssueOpen,
   126  								},
   127  							},
   128  							2: {
   129  								Issue: &gits.GitIssue{
   130  									Title: "Second issue",
   131  									State: &issues.IssueClosed,
   132  								},
   133  							},
   134  						},
   135  					},
   136  				},
   137  				dryRun:   false,
   138  				includes: nil,
   139  				excludes: nil,
   140  			},
   141  			wantErr:     false,
   142  			wantIssues:  true,
   143  			wantProject: false,
   144  			wantWiki:    false,
   145  		},
   146  		{
   147  			name: "keepProjectsOpen",
   148  			args: args{
   149  				repos: []repo{
   150  					{
   151  						GitRepository: gits.GitRepository{
   152  							Name:         "roadrunner",
   153  							Organisation: "acme",
   154  							HasIssues:    false,
   155  							HasWiki:      false,
   156  							HasProjects:  true,
   157  						},
   158  						projects: []gits.GitProject{
   159  							{
   160  								Name:        "Project 1",
   161  								Description: "",
   162  								Number:      1,
   163  								State:       "open",
   164  							},
   165  						},
   166  					},
   167  				},
   168  				dryRun:   false,
   169  				includes: nil,
   170  				excludes: nil,
   171  			},
   172  			wantErr:     false,
   173  			wantIssues:  false,
   174  			wantProject: true,
   175  			wantWiki:    false,
   176  		},
   177  		{
   178  			name: "keepWikiOpen",
   179  			args: args{
   180  				repos: []repo{
   181  					{
   182  						GitRepository: gits.GitRepository{
   183  							Name:         "roadrunner",
   184  							Organisation: "acme",
   185  							HasIssues:    false,
   186  							HasWiki:      true,
   187  							HasProjects:  false,
   188  						},
   189  						wikiEnabled: true,
   190  					},
   191  				},
   192  				dryRun:   false,
   193  				includes: nil,
   194  				excludes: nil,
   195  			},
   196  			wantErr:     false,
   197  			wantIssues:  false,
   198  			wantProject: false,
   199  			wantWiki:    true,
   200  		},
   201  	}
   202  	for _, tt := range ts {
   203  		t.Run(tt.name, func(t *testing.T) {
   204  			tests.Retry(t, 5, 5*time.Second, func(r *tests.R) {
   205  				fakeRepos := make([]*gits.FakeRepository, 0)
   206  				for _, r := range tt.args.repos {
   207  					fr, err := gits.NewFakeRepository(r.Organisation, r.Name, nil, nil)
   208  					assert.NoError(t, err)
   209  					fr.GitRepo.HasIssues = r.HasIssues
   210  					fr.GitRepo.HasWiki = r.HasWiki
   211  					fr.GitRepo.HasProjects = r.HasProjects
   212  					fr.Issues = r.issues
   213  					fr.Projects = r.projects
   214  					fr.WikiEnabled = r.wikiEnabled
   215  					fakeRepos = append(fakeRepos, fr)
   216  				}
   217  				provider := gits.NewFakeProvider(fakeRepos...)
   218  
   219  				terminal := tests.NewTerminal(t, nil)
   220  
   221  				handles := util.IOFileHandles{
   222  					Err: terminal.Err,
   223  					In:  terminal.In,
   224  					Out: terminal.Out,
   225  				}
   226  				err := DisableFeaturesForOrg("acme", tt.args.includes, tt.args.excludes, tt.args.dryRun, true, provider, handles)
   227  
   228  				if tt.wantErr {
   229  					assert.Error(t, err)
   230  				} else {
   231  					assert.NoError(t, err)
   232  				}
   233  				if err != nil {
   234  					return
   235  				}
   236  				for _, r := range tt.args.repos {
   237  					repo, err := provider.GetRepository(r.Organisation, r.Name)
   238  					assert.NoError(t, err)
   239  					if tt.wantIssues {
   240  						assert.True(t, repo.HasIssues)
   241  					}
   242  					if tt.wantProject {
   243  						assert.True(t, repo.HasProjects)
   244  					}
   245  					if tt.wantWiki {
   246  						assert.True(t, repo.HasWiki)
   247  					}
   248  				}
   249  
   250  			})
   251  		})
   252  	}
   253  }