src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/cli/lscolors/lscolors_test.go (about)

     1  package lscolors
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"src.elv.sh/pkg/testutil"
     8  )
     9  
    10  func TestLsColors(t *testing.T) {
    11  	SetTestLsColors(t)
    12  	testutil.InTempDir(t)
    13  	os.Mkdir("dir", 0755)
    14  	create("a.png")
    15  
    16  	colorist := GetColorist()
    17  
    18  	// Feature-based coloring.
    19  	wantDirStyle := "34"
    20  	if style := colorist.GetStyle("dir"); style != wantDirStyle {
    21  		t.Errorf("Got dir style %q, want %q", style, wantDirStyle)
    22  	}
    23  	// Extension-based coloring.
    24  	wantPngStyle := "31"
    25  	if style := colorist.GetStyle("a.png"); style != wantPngStyle {
    26  		t.Errorf("Got dir style %q, want %q", style, wantPngStyle)
    27  	}
    28  }
    29  
    30  func TestLsColors_SkipsInvalidFields(t *testing.T) {
    31  	testutil.Setenv(t, "LS_COLORS", "invalid=34:*.png=31")
    32  	testutil.InTempDir(t)
    33  	create("a.png")
    34  
    35  	wantPngStyle := "31"
    36  	if style := GetColorist().GetStyle("a.png"); style != wantPngStyle {
    37  		t.Errorf("Got dir style %q, want %q", style, wantPngStyle)
    38  	}
    39  }
    40  
    41  func TestLsColors_Default(t *testing.T) {
    42  	testutil.Setenv(t, "LS_COLORS", "")
    43  	testutil.InTempDir(t)
    44  	create("a.png")
    45  
    46  	// See defaultLsColorString
    47  	wantPngStyle := "01;35"
    48  	if style := GetColorist().GetStyle("a.png"); style != wantPngStyle {
    49  		t.Errorf("Got dir style %q, want %q", style, wantPngStyle)
    50  	}
    51  }