github.com/wawandco/oxplugins@v0.7.11/tools/docker/initializer_test.go (about)

     1  package docker
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func TestInitilizer(t *testing.T) {
    11  	t.Run("dockerFileDoesNotExist", func(t *testing.T) {
    12  
    13  		root := t.TempDir()
    14  		err := os.Chdir(root)
    15  		if err != nil {
    16  			t.Error("could not change to temp directory")
    17  		}
    18  
    19  		i := Initializer{}
    20  
    21  		err = i.Initialize(context.Background(), root, []string{})
    22  		if err != nil {
    23  			t.Fatalf("error should be nil, got %v", err)
    24  		}
    25  
    26  		rootDoc := root + "/.dockerignore"
    27  		rootFile := root + "/Dockerfile"
    28  
    29  		_, err = os.Stat(rootDoc)
    30  
    31  		if os.IsNotExist(err) {
    32  			t.Fatalf("Did not create .dockerignore file , %v", err)
    33  		}
    34  		_, err = os.Stat(rootFile)
    35  
    36  		if os.IsNotExist(err) {
    37  			t.Fatalf("Did not create  Dockerfile file , %v", err)
    38  		}
    39  	})
    40  
    41  	t.Run("dockerFileDoesExist", func(t *testing.T) {
    42  		root := t.TempDir()
    43  		err := os.Chdir(root)
    44  		if err != nil {
    45  			t.Error("could not change to temp directory")
    46  		}
    47  
    48  		rootFile := filepath.Join(root, "Dockerfile")
    49  		_, err = os.Create(rootFile)
    50  		if err != nil {
    51  			t.Fatalf("Error creating the file, %v", err)
    52  		}
    53  
    54  		i := Initializer{}
    55  
    56  		err = i.Initialize(context.Background(), root, []string{})
    57  		if err != nil {
    58  			t.Fatalf("error should be nil, got %v", err)
    59  		}
    60  		_, err = os.Stat(rootFile)
    61  
    62  		if os.IsNotExist(err) {
    63  			t.Fatalf("Did not create  Dockerfile file , %v", err)
    64  		}
    65  
    66  	})
    67  	t.Run("dockerIgnoreDoesExist", func(t *testing.T) {
    68  		root := t.TempDir()
    69  		err := os.Chdir(root)
    70  		if err != nil {
    71  			t.Error("could not change to temp directory")
    72  		}
    73  
    74  		rootdoc := filepath.Join(root, ".dockerignore")
    75  		_, err = os.Create(rootdoc)
    76  
    77  		if err != nil {
    78  			t.Fatalf("Error creating the file, %v", err)
    79  		}
    80  
    81  		i := Initializer{}
    82  
    83  		err = i.Initialize(context.Background(), root, []string{})
    84  		if err != nil {
    85  			t.Fatalf("error should be nil, got %v", err)
    86  		}
    87  		_, err = os.Stat(rootdoc)
    88  
    89  		if os.IsNotExist(err) {
    90  			t.Fatalf("Did not create .dockerignore file , %v", err)
    91  		}
    92  	})
    93  }