<commit_before>package test

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"sync"
	"testing"
	"time"

	"github.com/sacloud/libsacloud/v2"
	"github.com/sacloud/libsacloud/v2/sacloud"
	"github.com/sacloud/libsacloud/v2/sacloud/accessor"
	"github.com/sacloud/libsacloud/v2/sacloud/fake"
	"github.com/sacloud/libsacloud/v2/sacloud/trace"
	"github.com/sacloud/libsacloud/v2/sacloud/types"
)

var testZone string
var apiCaller sacloud.APICaller
var httpTrace bool

var accTestOnce sync.Once
var accTestMu sync.Mutex

func singletonAPICaller() sacloud.APICaller {
	accTestMu.Lock()
	defer accTestMu.Unlock()
	accTestOnce.Do(func() {
		//環境変数にトークン/シークレットがある場合のみテスト実施
		accessToken := os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
		accessTokenSecret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")

		if accessToken == "" || accessTokenSecret == "" {
			log.Println("Please Set ENV 'SAKURACLOUD_ACCESS_TOKEN' and 'SAKURACLOUD_ACCESS_TOKEN_SECRET'")
			os.Exit(0) // exit normal
		}
		client := sacloud.NewClient(accessToken, accessTokenSecret)
		client.DefaultTimeoutDuration = 30 * time.Minute
		client.UserAgent = fmt.Sprintf("test-libsacloud/%s", libsacloud.Version)
		client.AcceptLanguage = "en-US,en;q=0.9"

		client.RetryMax = 20
		client.RetryInterval = 3 * time.Second
		client.HTTPClient = &http.Client{}
		if httpTrace {
			client.HTTPClient.Transport = &sacloud.TracingRoundTripper{}
		}

		apiCaller = client
	})
	return apiCaller
}

func isAccTest() bool {
	return os.Getenv("TESTACC") != ""
}

func isEnableTrace() bool {
	return os.Getenv("SAKURACLOUD_TRACE") != ""
}

func isEnableAPITrace() bool {
	return os.Getenv("SAKURACLOUD_TRACE_API") != ""
}

func isEnableHTTPTrace() bool {
	return os.Getenv("SAKURACLOUD_TRACE_HTTP") != ""
}

func TestMain(m *testing.M) {
	testZone = os.Getenv("SAKURACLOUD_ZONE")
	if testZone == "" {
		testZone = "tk1v"
	}

	if !isAccTest() {<commit_msg>Use RateLimitRoundTripper when running tests<commit_after>package test

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"sync"
	"testing"
	"time"

	"github.com/sacloud/libsacloud/v2"
	"github.com/sacloud/libsacloud/v2/sacloud"
	"github.com/sacloud/libsacloud/v2/sacloud/accessor"
	"github.com/sacloud/libsacloud/v2/sacloud/fake"
	"github.com/sacloud/libsacloud/v2/sacloud/trace"
	"github.com/sacloud/libsacloud/v2/sacloud/types"
)

var testZone string
var apiCaller sacloud.APICaller
var httpTrace bool

var accTestOnce sync.Once
var accTestMu sync.Mutex

func singletonAPICaller() sacloud.APICaller {
	accTestMu.Lock()
	defer accTestMu.Unlock()
	accTestOnce.Do(func() {
		//環境変数にトークン/シークレットがある場合のみテスト実施
		accessToken := os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
		accessTokenSecret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")

		if accessToken == "" || accessTokenSecret == "" {
			log.Println("Please Set ENV 'SAKURACLOUD_ACCESS_TOKEN' and 'SAKURACLOUD_ACCESS_TOKEN_SECRET'")
			os.Exit(0) // exit normal
		}
		client := sacloud.NewClient(accessToken, accessTokenSecret)
		client.DefaultTimeoutDuration = 30 * time.Minute
		client.UserAgent = fmt.Sprintf("test-libsacloud/%s", libsacloud.Version)
		client.AcceptLanguage = "en-US,en;q=0.9"

		client.RetryMax = 20
		client.RetryInterval = 3 * time.Second
		client.HTTPClient = &http.Client{
			Transport: &sacloud.RateLimitRoundTripper{RateLimitPerSec: 1},
		}
		if httpTrace {
			client.HTTPClient.Transport = &sacloud.TracingRoundTripper{
				Transport: client.HTTPClient.Transport,
			}
		}

		apiCaller = client
	})
	return apiCaller
}

func isAccTest() bool {
	return os.Getenv("TESTACC") != ""
}

func isEnableTrace() bool {
	return os.Getenv("SAKURACLOUD_TRACE") != ""
}

func isEnableAPITrace() bool {
	return os.Getenv("SAKURACLOUD_TRACE_API") != ""
}

func isEnableHTTPTrace() bool {
	return os.Getenv("SAKURACLOUD_TRACE_HTTP") != ""
}

func TestMain(m *testing.M) {
	testZone = os.Getenv("SAKURACLOUD_ZONE")
	if testZone == "" {
		testZone = "tk1v"
	}

	if !isAccTest() {
{'fraction_non_alphanumeric': 0.13511450381679388, 'fraction_numerical': 0.007888040712468193, 'mean_word_length': 4.858420268256334, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3489349', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1619, 'n_words': 235}