<commit_before>// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package project

import (
	"encoding/xml"
	"fmt"
	"os"
	"path/filepath"

	"v.io/jiri/collect"
	"v.io/jiri/tool"
)

type FakeJiriRoot struct {
	Dir      string
	Projects map[string]string
	remote   string
}

const (
	defaultDataDir  = "data"
	defaultManifest = "default"
	manifestProject = ".manifest"
	manifestVersion = "v2"
	toolsProject    = "tools"
)

// NewFakeJiriRoot is the FakeJiriRoot factory.
func NewFakeJiriRoot(ctx *tool.Context) (*FakeJiriRoot, error) {
	root := &FakeJiriRoot{
		Projects: map[string]string{},
	}

	// Create fake remote manifest and tools projects.
	remoteDir, err := ctx.Run().TempDir("", "")
	if err != nil {
		return nil, err
	}
	root.remote = remoteDir
	if err := root.CreateRemoteProject(ctx, manifestProject); err != nil {
		return nil, err
	}
	if err := root.CreateRemoteProject(ctx, toolsProject); err != nil {
		return nil, err
	}

	// Create a fake manifest.
	manifestDir := filepath.Join(remoteDir, manifestProject, manifestVersion)
	if err := ctx.Run().MkdirAll(manifestDir, os.FileMode(0700)); err != nil {
		return nil, err
	}
	if err := root.WriteRemoteManifest(ctx, &Manifest{}); err != nil {
		return nil, err
	}

	// Create a fake JIRI_ROOT.
	rootDir, err := ctx.Run().TempDir("", "")
	if err != nil {
		return nil, err
	}
	root.Dir = rootDir
	if err := ctx.Git().Clone(root.Projects[manifestProject], filepath.Join(root.Dir, manifestProject)); err != nil {
		return nil, err
	}

	// Add the "tools" project and a fake "jiri" tool to the
	// manifests. This is necessary to make sure that the commonly
	// invoked DataDirPath() function, which uses the "jiri" tool
	// configuration for its default, works.
	if err := root.AddProject(ctx, Project{
		Name:   toolsProject,
		Path:   toolsProject,
		Remote: root.Projects[toolsProject],
	}); err != nil {
		return nil, err
	}
	if err := root.AddTool(ctx, Tool{
		Name:    "jiri",
		Data:    defaultDataDir,
		Project: toolsProject,
	}); err != nil {
		return nil, err
	}

	// Add "gerrit" and "git" hosts to the manifest, as required by the "jiri"
	// tool.
	if err := root.AddHost(ctx, Host{
		Name:     "gerrit",
		Location: "git://example.com/gerrit",
	}); err != nil {
		return nil, err
	}
	if err := root.AddHost(ctx, Host{
		Name:     "git",
		Location: "git://example.com/git",
	}); err != nil {
		return nil, err
	}

	// Update the contents of the fake JIRI_ROOT instance based on
	// the information recorded in the remote manifest.
	if err := root.UpdateUniverse(ctx, false); err != nil {
		return nil, err
	}

	return root, nil
}

// Cleanup cleans up the given Vanadium root fake.
func (root FakeJiriRoot) Cleanup(ctx *tool.Context) error {
	var errs []error
	collect.Errors(func() error { return ctx.Run().RemoveAll(root.Dir) }, &errs)
	collect.Errors(func() error { return ctx.Run().RemoveAll(root.remote) }, &errs)
	if len(errs) != 0 {<commit_msg>jiri: Cleanup fake jiri dir if some step of creation failed<commit_after>// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package project

import (
	"encoding/xml"
	"fmt"
	"os"
	"path/filepath"

	"v.io/jiri/collect"
	"v.io/jiri/tool"
)

type FakeJiriRoot struct {
	Dir      string
	Projects map[string]string
	remote   string
}

const (
	defaultDataDir  = "data"
	defaultManifest = "default"
	manifestProject = ".manifest"
	manifestVersion = "v2"
	toolsProject    = "tools"
)

// NewFakeJiriRoot is the FakeJiriRoot factory.
func NewFakeJiriRoot(ctx *tool.Context) (*FakeJiriRoot, error) {
	root := &FakeJiriRoot{
		Projects: map[string]string{},
	}

	// Create fake remote manifest and tools projects.
	remoteDir, err := ctx.Run().TempDir("", "")
	if err != nil {
		return nil, err
	}
	doCleanup := true
	defer func() {
		if doCleanup {
			root.Cleanup(ctx)
		}
	}()

	root.remote = remoteDir
	if err := root.CreateRemoteProject(ctx, manifestProject); err != nil {
		return nil, err
	}
	if err := root.CreateRemoteProject(ctx, toolsProject); err != nil {
		return nil, err
	}

	// Create a fake manifest.
	manifestDir := filepath.Join(remoteDir, manifestProject, manifestVersion)
	if err := ctx.Run().MkdirAll(manifestDir, os.FileMode(0700)); err != nil {
		return nil, err
	}
	if err := root.WriteRemoteManifest(ctx, &Manifest{}); err != nil {
		return nil, err
	}

	// Create a fake JIRI_ROOT.
	rootDir, err := ctx.Run().TempDir("", "")
	if err != nil {
		return nil, err
	}
	root.Dir = rootDir
	if err := ctx.Git().Clone(root.Projects[manifestProject], filepath.Join(root.Dir, manifestProject)); err != nil {
		return nil, err
	}

	// Add the "tools" project and a fake "jiri" tool to the
	// manifests. This is necessary to make sure that the commonly
	// invoked DataDirPath() function, which uses the "jiri" tool
	// configuration for its default, works.
	if err := root.AddProject(ctx, Project{
		Name:   toolsProject,
		Path:   toolsProject,
		Remote: root.Projects[toolsProject],
	}); err != nil {
		return nil, err
	}
	if err := root.AddTool(ctx, Tool{
		Name:    "jiri",
		Data:    defaultDataDir,
		Project: toolsProject,
	}); err != nil {
		return nil, err
	}

	// Add "gerrit" and "git" hosts to the manifest, as required by the "jiri"
	// tool.
	if err := root.AddHost(ctx, Host{
		Name:     "gerrit",
		Location: "git://example.com/gerrit",
	}); err != nil {
		return nil, err
	}
	if err := root.AddHost(ctx, Host{
		Name:     "git",
		Location: "git://example.com/git",
	}); err != nil {
		return nil, err
	}

	// Update the contents of the fake JIRI_ROOT instance based on
	// the information recorded in the remote manifest.
	if err := root.UpdateUniverse(ctx, false); err != nil {
		return nil, err
	}

	doCleanup = false
	return root, nil
}

// Cleanup cleans up the given Vanadium root fake.
func (root FakeJiriRoot) Cleanup(ctx *tool.Context) error {
	var errs []error
	collect.Errors(func() error {
		if root.Dir == "" {
			return nil
		}
		return ctx.Run().RemoveAll(root.Dir)
	}, &errs)
	collect.Errors(func() error { return ctx.Run().RemoveAll(root.remote) }, &errs)
	if len(errs) != 0 {
{'fraction_non_alphanumeric': 0.13645783516658697, 'fraction_numerical': 0.003188267176789415, 'mean_word_length': 3.526695526695527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3462682', 'n_tokens_mistral': 2250, 'n_tokens_neox': 2143, 'n_words': 665}