go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/lucicfg/testdata/misc/vars.star (about) 1 # Prepare CLI vars as: 2 # 3 # test_var=abc 4 # another_var=def 5 6 load("//testdata/misc/support/shared_vars.star", "shared_vars") 7 8 def test_vars_basics(): 9 a = lucicfg.var() 10 a.set(123) 11 assert.eq(a.get(), 123) 12 13 # Setting again is forbidden. 14 assert.fails(lambda: a.set(123), "variable reassignment is forbidden") 15 16 def test_vars_defaults(): 17 a = lucicfg.var(default = 123) 18 assert.eq(a.get(), 123) 19 20 # Setting a var after it was auto-initialized is forbidden. 21 assert.fails(lambda: a.set(None), "variable reassignment is forbidden") 22 23 def test_vars_validator(): 24 a = lucicfg.var(validator = lambda v: v + 1) 25 a.set(1) 26 assert.eq(a.get(), 2) 27 28 def test_propagation_down_exec_stack(): 29 # Set 'a' only. 30 shared_vars.a.set("from root") 31 32 # The execed script should be able to read 'a' and set 'b'. 33 out = exec("//testdata/misc/support/uses_vars.star") 34 assert.eq(out.sees_vars, ["from root", "from inner"]) 35 36 # But the mutation to 'b' didn't propagate back to us. 37 assert.eq(shared_vars.b.get(), None) 38 39 def test_expose_as_works(): 40 # See the top of this file for where 'abc' is set. 41 assert.eq(lucicfg.var(expose_as = "test_var").get(), "abc") 42 43 # Default works. 44 assert.eq(lucicfg.var(expose_as = "some", default = "zzz").get(), "zzz") 45 46 # 'None' default also works. 47 assert.eq(lucicfg.var(expose_as = "third").get(), None) 48 49 def test_expose_as_set_fails(): 50 assert.fails( 51 lambda: lucicfg.var(expose_as = "v1").set("123"), 52 "the value of the variable is controlled through CLI flag " + 53 r'\"\-var v1=..." and can\'t be changed from Starlark side', 54 ) 55 56 def test_expose_as_bad_default_type(): 57 assert.fails( 58 lambda: lucicfg.var(expose_as = "v2", default = 123), 59 "must have a string or None default, got int 123", 60 ) 61 62 def test_expose_as_duplicate(): 63 v1 = lucicfg.var(expose_as = "another_var") 64 v2 = lucicfg.var(expose_as = "another_var") 65 assert.eq(v1.get(), "def") 66 assert.eq(v2.get(), "def") 67 68 # Still "physically" different vars though, just have the exact same value. 69 assert.true(v1 != v2) 70 71 test_vars_basics() 72 test_vars_defaults() 73 test_vars_validator() 74 test_propagation_down_exec_stack() 75 test_expose_as_works() 76 test_expose_as_set_fails() 77 test_expose_as_bad_default_type() 78 test_expose_as_duplicate()