github.com/yourbase/yb@v0.7.1/cmd/yb/init_test.go (about)

     1  // Copyright 2020 YourBase Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //		 https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // SPDX-License-Identifier: Apache-2.0
    16  
    17  package main
    18  
    19  import (
    20  	"context"
    21  	"io/fs"
    22  	"io/ioutil"
    23  	"os"
    24  	slashpath "path"
    25  	"path/filepath"
    26  	"testing"
    27  
    28  	"github.com/yourbase/yb"
    29  	"zombiezen.com/go/log/testlog"
    30  )
    31  
    32  func TestDetectLanguage(t *testing.T) {
    33  	tests := []struct {
    34  		name  string
    35  		files []string
    36  		want  string
    37  	}{
    38  		{
    39  			name:  "EmptyDir",
    40  			files: []string{},
    41  			want:  langGenericFlagValue,
    42  		},
    43  		{
    44  			name: "Go/Mod",
    45  			files: []string{
    46  				"go.mod",
    47  			},
    48  			want: langGoFlagValue,
    49  		},
    50  		{
    51  			name: "Go/JustSource",
    52  			files: []string{
    53  				"xyzzy.go",
    54  			},
    55  			want: langGoFlagValue,
    56  		},
    57  		{
    58  			name: "Python/PipRequirements",
    59  			files: []string{
    60  				"requirements.txt",
    61  			},
    62  			want: langPythonFlagValue,
    63  		},
    64  		{
    65  			name: "Python/Setuptools",
    66  			files: []string{
    67  				"setup.py",
    68  			},
    69  			want: langPythonFlagValue,
    70  		},
    71  		{
    72  			name: "Ruby",
    73  			files: []string{
    74  				"Gemfile",
    75  			},
    76  			want: langRubyFlagValue,
    77  		},
    78  		{
    79  			name: "Makefile",
    80  			files: []string{
    81  				"Makefile",
    82  			},
    83  			want: langGenericFlagValue,
    84  		},
    85  		{
    86  			name: "MultipleLanguages",
    87  			files: []string{
    88  				"go.mod",
    89  				"setup.py",
    90  			},
    91  			want: langGenericFlagValue,
    92  		},
    93  	}
    94  	for _, test := range tests {
    95  		t.Run(test.name, func(t *testing.T) {
    96  			ctx := testlog.WithTB(context.Background(), t)
    97  			dir := t.TempDir()
    98  			for _, fname := range test.files {
    99  				dst := filepath.Join(dir, filepath.FromSlash(fname))
   100  				if err := os.MkdirAll(filepath.Dir(dst), 0o777); err != nil {
   101  					t.Fatal(err)
   102  				}
   103  				if err := ioutil.WriteFile(dst, nil, 0o666); err != nil {
   104  					t.Fatal(err)
   105  				}
   106  			}
   107  			got, err := detectLanguage(ctx, dir)
   108  			if got != test.want || err != nil {
   109  				t.Errorf("detectLanguage(%q) = %q, %v; want %q, <nil>", dir, got, err, test.want)
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  func TestPackageConfigTemplates(t *testing.T) {
   116  	const dir = "init_templates"
   117  	entries, err := fs.ReadDir(packageConfigTemplateFiles, dir)
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  	for _, ent := range entries {
   122  		name := ent.Name()
   123  		t.Run(name, func(t *testing.T) {
   124  			dst := filepath.Join(t.TempDir(), yb.PackageConfigFilename)
   125  			data, err := fs.ReadFile(packageConfigTemplateFiles, slashpath.Join(dir, name))
   126  			if err != nil {
   127  				t.Fatal(err)
   128  			}
   129  			if err := ioutil.WriteFile(dst, []byte(data), 0o666); err != nil {
   130  				t.Fatal(err)
   131  			}
   132  			if _, err := yb.LoadPackage(dst); err != nil {
   133  				t.Errorf("Load file: %v", err)
   134  			}
   135  		})
   136  	}
   137  }