# Smoke test that we can obtain real stats from the OS.
# Go stats should work on all platforms; Proc stats are OS-specific.
env CUE_STATS_FILE=-
exec cue export x.cue
stderr -count=1 '"EvalVersion": 3,'
stderr -count=1 '"AllocBytes": [1-9]\d*,'
[unix] stderr -count=1 '"MaxRssBytes": [1-9]\d*$'
[windows] stderr -count=1 '"MaxRssBytes": 0$'

! exec cue vet -c fail.cue
stderr -count=1 '"EvalVersion": 3,'
stderr 'a: conflicting values'

env CUE_STATS_FILE=

# Smoke test that CUE_BENCH also works.
env CUE_BENCH=FooBar
exec cue export x.cue
! stderr .
[unix] stdout -count=1 '^BenchmarkFooBar\s+1\s+[1-9]\d* ns/op'
stdout -count=1 '\d+\s+B/op'
stdout -count=1 '\d+\s+max-RSS-B/op'

! exec cue vet -c fail.cue
stdout -count=1 '\d+\s+B/op'
stderr 'a: conflicting values'

env CUE_BENCH=

# Mock the Go runtime and OS stats, to verify the rest of the logic.
env CUE_TEST_MEMSTATS=memstats.json
env CUE_TEST_SYSUSAGE=sysusage.json

env CUE_STATS_FILE=stats.json
exec cue export x.cue
cmp stats.json out/stats.json

# overwrite existing files.
env CUE_STATS_FILE=overwrite_stats.json
exec cue export x.cue
cmp overwrite_stats.json out/stats.json

env CUE_STATS_FILE=stats.cue
exec cue export x.cue
cmp stats.cue out/stats.cue

env CUE_STATS_FILE=stats.yaml
exec cue export x.cue
cmp stats.yaml out/stats.yaml

# default output is JSON.
env CUE_STATS_FILE=-
exec cue export x.cue
cmp stderr out/stderr

-- x.cue --
a: 1
b: 2
c: a | *b
-- fail.cue --
a: 1 & 2
-- memstats.json --
{
    "TotalAlloc": 300456,
    "Mallocs": 100123
}
-- sysusage.json --
{
    "UserNano": 123000,
    "SysNano": 456000,
    "MaxRssBytes": 789000
}
-- overwrite_stats.json --
contents overwritten
-- out/stats.json --
{
    "CUE": {
        "EvalVersion": 3,
        "Unifications": 4,
        "Disjuncts": 2,
        "Notifications": 0,
        "Conjuncts": 8,
        "NumCloseIDs": 2,
        "ConjunctInfos": 6,
        "MaxConjunctInfos": 2,
        "MaxReqSets": 0,
        "MaxRedirect": 0,
        "GenerationMismatch": 0,
        "MisalignedConjunct": 0,
        "MisalignedConstraint": 0,
        "SkippedNotification": 0,
        "ResolveDep": 0,
        "Freed": 6,
        "Reused": 0,
        "Allocs": 6,
        "Retained": 0
    },
    "Go": {
        "AllocBytes": 300456,
        "AllocObjects": 100123
    },
    "Proc": {
        "UserNano": 123000,
        "SysNano": 456000,
        "MaxRssBytes": 789000
    }
}
-- out/stats.cue --
CUE: {
	EvalVersion:          3
	Unifications:         4
	Disjuncts:            2
	Notifications:        0
	Conjuncts:            8
	NumCloseIDs:          2
	ConjunctInfos:        6
	MaxConjunctInfos:     2
	MaxReqSets:           0
	MaxRedirect:          0
	GenerationMismatch:   0
	MisalignedConjunct:   0
	MisalignedConstraint: 0
	SkippedNotification:  0
	ResolveDep:           0
	Freed:                6
	Reused:               0
	Allocs:               6
	Retained:             0
}
Go: {
	AllocBytes:   300456
	AllocObjects: 100123
}
Proc: {
	UserNano:    123000
	SysNano:     456000
	MaxRssBytes: 789000
}
-- out/stats.yaml --
CUE:
  EvalVersion: 3
  Unifications: 4
  Disjuncts: 2
  Notifications: 0
  Conjuncts: 8
  NumCloseIDs: 2
  ConjunctInfos: 6
  MaxConjunctInfos: 2
  MaxReqSets: 0
  MaxRedirect: 0
  GenerationMismatch: 0
  MisalignedConjunct: 0
  MisalignedConstraint: 0
  SkippedNotification: 0
  ResolveDep: 0
  Freed: 6
  Reused: 0
  Allocs: 6
  Retained: 0
Go:
  AllocBytes: 300456
  AllocObjects: 100123
Proc:
  UserNano: 123000
  SysNano: 456000
  MaxRssBytes: 789000
-- out/stderr --
{
    "CUE": {
        "EvalVersion": 3,
        "Unifications": 4,
        "Disjuncts": 2,
        "Notifications": 0,
        "Conjuncts": 8,
        "NumCloseIDs": 2,
        "ConjunctInfos": 6,
        "MaxConjunctInfos": 2,
        "MaxReqSets": 0,
        "MaxRedirect": 0,
        "GenerationMismatch": 0,
        "MisalignedConjunct": 0,
        "MisalignedConstraint": 0,
        "SkippedNotification": 0,
        "ResolveDep": 0,
        "Freed": 6,
        "Reused": 0,
        "Allocs": 6,
        "Retained": 0
    },
    "Go": {
        "AllocBytes": 300456,
        "AllocObjects": 100123
    },
    "Proc": {
        "UserNano": 123000,
        "SysNano": 456000,
        "MaxRssBytes": 789000
    }
}
