Persist persists the environment stored in context in a ".persisted" file as JSON
(ctx context.Context)
| 63 | |
| 64 | // Persist persists the environment stored in context in a ".persisted" file as JSON |
| 65 | func Persist(ctx context.Context) (bool, error) { |
| 66 | if !Persisted(ctx) { |
| 67 | return false, nil |
| 68 | } |
| 69 | |
| 70 | values, ok := ctx.Value(persistedEnv).(*map[string]any) |
| 71 | if !ok { |
| 72 | return true, errors.New("did not find expected map type in Context for the persistedEnv value") |
| 73 | } |
| 74 | |
| 75 | b, err := json.Marshal(values) |
| 76 | if err != nil { |
| 77 | return true, fmt.Errorf("unable to store JSON data in .persisted file: %v", err.Error()) |
| 78 | } |
| 79 | |
| 80 | err = persister(persistedFile, b, 0o644) |
| 81 | if err != nil { |
| 82 | return true, fmt.Errorf("unable to write to %s file: %v", persistedFile, err.Error()) |
| 83 | } |
| 84 | |
| 85 | return true, nil |
| 86 | } |
| 87 | |
| 88 | // Persisted returns true if the test environment persists after the test has finished |
| 89 | func Persisted(ctx context.Context) bool { |