go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/lucicfg/testdata/misc/graph.star (about)

     1  load("@stdlib//internal/graph.star", "graph")
     2  
     3  # Detailed unit tests for graph are in 'graph' go package. Here we only test
     4  # that graph.star wrapper and integration with the generator state works.
     5  
     6  root = graph.key("root", "root")
     7  graph.add_node(root, props = {"msg": "Hey"})
     8  
     9  def child(id, msg, parent = root):
    10      k = graph.key("child", id)
    11      graph.add_node(k, props = {"msg": msg})
    12      graph.add_edge(parent, k)
    13      return k
    14  
    15  c1 = child("c1", "hello")
    16  c2 = child("c2", "world")
    17  
    18  c3 = child("c3", "deeper", parent = c1)
    19  c4 = child("c4", "more deeper", parent = c3)
    20  c5 = child("c5", "deeeep", parent = c4)
    21  
    22  def gen1(ctx):
    23      msg = []
    24      for c in graph.children(root, "child"):
    25          msg.append("%s: %s" % (c.props.msg, graph.parents(c.key, "root")))
    26      ctx.output["children_parents.txt"] = "\n".join(msg) + "\n"
    27  
    28  lucicfg.generator(impl = gen1)
    29  
    30  def gen2(ctx):
    31      ctx.output["recursive.txt"] = " ".join([
    32          n.props.msg
    33          for n in graph.sorted_nodes(
    34              graph.descendants(root),
    35              graph.DEFINITION_ORDER,
    36          )
    37      ]) + "\n"
    38  
    39  lucicfg.generator(impl = gen2)
    40  
    41  # Expect configs:
    42  #
    43  # === children_parents.txt
    44  # hello: [root("root")]
    45  # world: [root("root")]
    46  # ===
    47  #
    48  # === recursive.txt
    49  # Hey hello world deeper more deeper deeeep
    50  # ===