1+ package main
2+
3+ import (
4+ "testing"
5+ )
6+
7+ func TestCompareEnvFiles (t * testing.T ) {
8+ t .Run ("detect missing variables" , func (t * testing.T ) {
9+ env1 := & EnvFile {
10+ Path : "file1" ,
11+ Vars : map [string ]EnvVar {
12+ "KEY1" : {Key : "KEY1" , Value : "val1" , Line : 1 },
13+ },
14+ }
15+ env2 := & EnvFile {
16+ Path : "file2" ,
17+ Vars : map [string ]EnvVar {
18+ "KEY1" : {Key : "KEY1" , Value : "val1" , Line : 1 },
19+ "KEY2" : {Key : "KEY2" , Value : "val2" , Line : 2 },
20+ },
21+ }
22+
23+ diff := CompareEnvFiles (env1 , env2 , "file1" , "file2" )
24+
25+ if len (diff .Missing ) != 1 {
26+ t .Errorf ("expected 1 missing var, got %d" , len (diff .Missing ))
27+ }
28+ if diff .Missing [0 ].Key != "KEY2" {
29+ t .Errorf ("expected missing KEY2, got %s" , diff .Missing [0 ].Key )
30+ }
31+ })
32+
33+ t .Run ("detect extra variables" , func (t * testing.T ) {
34+ env1 := & EnvFile {
35+ Path : "file1" ,
36+ Vars : map [string ]EnvVar {
37+ "KEY1" : {Key : "KEY1" , Value : "val1" , Line : 1 },
38+ "KEY2" : {Key : "KEY2" , Value : "val2" , Line : 2 },
39+ },
40+ }
41+ env2 := & EnvFile {
42+ Path : "file2" ,
43+ Vars : map [string ]EnvVar {
44+ "KEY1" : {Key : "KEY1" , Value : "val1" , Line : 1 },
45+ },
46+ }
47+
48+ diff := CompareEnvFiles (env1 , env2 , "file1" , "file2" )
49+
50+ if len (diff .Extra ) != 1 {
51+ t .Errorf ("expected 1 extra var, got %d" , len (diff .Extra ))
52+ }
53+ })
54+
55+ t .Run ("detect mismatched values" , func (t * testing.T ) {
56+ env1 := & EnvFile {
57+ Path : "file1" ,
58+ Vars : map [string ]EnvVar {
59+ "KEY1" : {Key : "KEY1" , Value : "val1" , Line : 1 },
60+ },
61+ }
62+ env2 := & EnvFile {
63+ Path : "file2" ,
64+ Vars : map [string ]EnvVar {
65+ "KEY1" : {Key : "KEY1" , Value : "val2" , Line : 1 },
66+ },
67+ }
68+
69+ diff := CompareEnvFiles (env1 , env2 , "file1" , "file2" )
70+
71+ if len (diff .Mismatched ) != 1 {
72+ t .Errorf ("expected 1 mismatch, got %d" , len (diff .Mismatched ))
73+ }
74+ })
75+
76+ t .Run ("identical files" , func (t * testing.T ) {
77+ env1 := & EnvFile {
78+ Path : "file1" ,
79+ Vars : map [string ]EnvVar {
80+ "KEY1" : {Key : "KEY1" , Value : "val1" , Line : 1 },
81+ },
82+ }
83+ env2 := & EnvFile {
84+ Path : "file2" ,
85+ Vars : map [string ]EnvVar {
86+ "KEY1" : {Key : "KEY1" , Value : "val1" , Line : 1 },
87+ },
88+ }
89+
90+ diff := CompareEnvFiles (env1 , env2 , "file1" , "file2" )
91+
92+ if diff .HasDifferences () {
93+ t .Error ("expected no differences for identical files" )
94+ }
95+ if len (diff .Matching ) != 1 {
96+ t .Errorf ("expected 1 matching var, got %d" , len (diff .Matching ))
97+ }
98+ })
99+ }
100+
101+ func TestHasDifferences (t * testing.T ) {
102+ tests := []struct {
103+ name string
104+ diff * DiffResult
105+ want bool
106+ }{
107+ {"no diffs" , & DiffResult {}, false },
108+ {"has missing" , & DiffResult {Missing : []EnvVar {{Key : "K" }}}, true },
109+ {"has extra" , & DiffResult {Extra : []EnvVar {{Key : "K" }}}, true },
110+ {"has mismatch" , & DiffResult {Mismatched : []MismatchedVar {{Key : "K" }}}, true },
111+ }
112+
113+ for _ , tt := range tests {
114+ t .Run (tt .name , func (t * testing.T ) {
115+ if got := tt .diff .HasDifferences (); got != tt .want {
116+ t .Errorf ("HasDifferences() = %v, want %v" , got , tt .want )
117+ }
118+ })
119+ }
120+ }
0 commit comments