github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/pkg/snapshot/version_test.go (about)

     1  package snapshot
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  
     9  	"golang.org/x/xerrors"
    10  )
    11  
    12  func TestGetVersionFromProdSnapshot(t *testing.T) {
    13  	snapshot := map[string]interface{}{
    14  		"View": map[string]interface{}{
    15  			"RunningTiltBuild": map[string]interface{}{
    16  				"Version": "0.7.13",
    17  				"Dev":     false,
    18  			},
    19  		},
    20  	}
    21  
    22  	expected := "v0.7.13"
    23  	actual, err := GetVersionFromSnapshot(snapshot)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	if expected != actual {
    29  		t.Errorf("Expected %s, got %s", expected, actual)
    30  	}
    31  }
    32  
    33  func TestGetVersionFromUISession(t *testing.T) {
    34  	snapshot := map[string]interface{}{
    35  		"view": map[string]interface{}{
    36  			"uiSession": map[string]interface{}{
    37  				"status": map[string]interface{}{
    38  					"runningTiltBuild": map[string]interface{}{
    39  						"version": "0.20.2",
    40  					},
    41  				},
    42  			},
    43  		},
    44  	}
    45  
    46  	expected := "v0.20.2"
    47  	actual, err := GetVersionFromSnapshot(snapshot)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  
    52  	if expected != actual {
    53  		t.Errorf("Expected %s, got %s", expected, actual)
    54  	}
    55  }
    56  
    57  func TestGetVersionFromProdSnapshotV2(t *testing.T) {
    58  	snapshot := map[string]interface{}{
    59  		"view": map[string]interface{}{
    60  			"runningTiltBuild": map[string]interface{}{
    61  				"version": "0.7.13",
    62  				"dev":     false,
    63  			},
    64  		},
    65  	}
    66  
    67  	expected := "v0.7.13"
    68  	actual, err := GetVersionFromSnapshot(snapshot)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	if expected != actual {
    74  		t.Errorf("Expected %s, got %s", expected, actual)
    75  	}
    76  }
    77  
    78  func TestGetVersionFromDevSnapshot(t *testing.T) {
    79  	snapshot := map[string]interface{}{
    80  		"View": map[string]interface{}{
    81  			"RunningTiltBuild": map[string]interface{}{
    82  				"Version":   "v0.10.13",
    83  				"Dev":       true,
    84  				"CommitSHA": "aaaaaa",
    85  			},
    86  		},
    87  	}
    88  
    89  	expected := "aaaaaa"
    90  	actual, err := GetVersionFromSnapshot(snapshot)
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	if expected != actual {
    96  		t.Errorf("Expected %s, got %s", expected, actual)
    97  	}
    98  }
    99  
   100  func TestGetVersionFromDevSnapshotV2(t *testing.T) {
   101  	snapshot := map[string]interface{}{
   102  		"view": map[string]interface{}{
   103  			"runningTiltBuild": map[string]interface{}{
   104  				"version":   "v0.10.13",
   105  				"dev":       true,
   106  				"commitSHA": "aaaaaa",
   107  			},
   108  		},
   109  	}
   110  
   111  	expected := "aaaaaa"
   112  	actual, err := GetVersionFromSnapshot(snapshot)
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  
   117  	if expected != actual {
   118  		t.Errorf("Expected %s, got %s", expected, actual)
   119  	}
   120  }
   121  
   122  func TestGetVersionFromDevSnapshotDevIsFalse(t *testing.T) {
   123  	snapshot := map[string]interface{}{
   124  		"View": map[string]interface{}{
   125  			"RunningTiltBuild": map[string]interface{}{
   126  				"Version":   "0.10.13",
   127  				"Dev":       false,
   128  				"CommitSHA": "aaaaaa",
   129  			},
   130  		},
   131  	}
   132  
   133  	expected := "v0.10.13"
   134  	actual, err := GetVersionFromSnapshot(snapshot)
   135  	if err != nil {
   136  		t.Fatal(err)
   137  	}
   138  
   139  	if expected != actual {
   140  		t.Errorf("Expected %s, got %s", expected, actual)
   141  	}
   142  }
   143  
   144  func TestGetEmptyVersionFromMalformedSnapshot(t *testing.T) {
   145  	snapshot := map[string]interface{}{
   146  		"Foo": map[string]interface{}{
   147  			"Bar": "",
   148  		},
   149  	}
   150  
   151  	expected := ""
   152  	actual, err := GetVersionFromSnapshot(snapshot)
   153  	if err == nil {
   154  		t.Fatal("expected error")
   155  	}
   156  
   157  	if expected != actual {
   158  		t.Errorf("Expected %s, got %s", expected, actual)
   159  	}
   160  }
   161  
   162  func TestShouldNotParseAsProtobuf(t *testing.T) {
   163  	b, err := os.ReadFile("testdata/snapshot.json")
   164  	if err != nil {
   165  		t.Fatal(err)
   166  	}
   167  
   168  	shouldParse, err := shouldParseAsProtobuf(b)
   169  	if err != nil {
   170  		t.Fatal(err)
   171  	}
   172  
   173  	if shouldParse != false {
   174  		t.Errorf("Expected shouldParse to be false, got true")
   175  	}
   176  }
   177  
   178  func TestShouldParseAsProtobuf(t *testing.T) {
   179  	b, err := os.ReadFile("testdata/snapshot_new.json")
   180  	if err != nil {
   181  		t.Fatal(err)
   182  	}
   183  
   184  	shouldParse, err := shouldParseAsProtobuf(b)
   185  	if err != nil {
   186  		t.Fatal(err)
   187  	}
   188  
   189  	if shouldParse != true {
   190  		t.Errorf("Expected shouldParse to be true, got false")
   191  	}
   192  }
   193  
   194  func shouldParseAsProtobuf(b []byte) (bool, error) {
   195  	snapMap := make(map[string]interface{})
   196  
   197  	err := json.Unmarshal(b, &snapMap)
   198  	if err != nil {
   199  		msg := fmt.Sprintf("Error decoding snapshot as JSON: %v\n", err)
   200  		err = xerrors.New(msg)
   201  		return false, err
   202  	}
   203  
   204  	_, isV2 := snapMap["view"]
   205  
   206  	return isV2, nil
   207  }