<commit_before>package main_test

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
)

func TestHwafTuto(t *testing.T) {
	workdir, err := ioutil.TempDir("", "hwaf-test-")
	if err != nil {
		t.Fatalf(err.Error())
	}
	defer os.RemoveAll(workdir)

	err = os.Chdir(workdir)
	if err != nil {
		t.Fatalf(err.Error())
	}

	hwaf, err := newlogger("hwaf.log")
	if err != nil {
		t.Fatalf(err.Error())
	}
	defer hwaf.Close()

	for _, cmd := range [][]string{
		{"hwaf", "init", "-q=0", "."},
		{"hwaf", "setup", "-q=0"},
		{"hwaf", "pkg", "create", "-q=0", "mytools/mypkg"},
		{"hwaf", "pkg", "ls"},
	} {
		err := hwaf.Run(cmd[0], cmd[1:]...)
		if err != nil {
			hwaf.Display()
			t.Fatalf("cmd %v failed: %v", cmd, err)
		}
	}

	mypkgdir := filepath.Join("src", "mytools", "mypkg")

	// create src/mytools/mypkg/wscript file
	ff, err := os.Create(filepath.Join(mypkgdir, "wscript"))
	if err != nil {
		t.Fatalf(err.Error())
	}
	_, err = ff.WriteString(
		`
# -*- python -*-
# automatically generated wscript

import waflib.Logs as msg

PACKAGE = {
    'name': 'mytools/mypkg',
    'author': ["<NAME>"], 
}

def pkg_deps(ctx):
    # put your package dependencies here.
    # e.g.:
    # ctx.use_pkg('AtlasPolicy')
    return

def configure(ctx):
    ctx.load('find_clhep')
    ctx.find_clhep(mandatory=False)
    ctx.start_msg("was clhep found ?")
    ctx.end_msg(ctx.env.HWAF_FOUND_CLHEP)
    if ctx.env.HWAF_FOUND_CLHEP:
        ctx.start_msg("clhep version")
        ctx.end_msg(ctx.env.CLHEP_VERSION)
        msg.info("clhep linkflags: %s" % ctx.env['LINKFLAGS_CLHEP'])
        msg.info("clhep cxxflags: %s" % ctx.env['CXXFLAGS_CLHEP'])
    
    from waflib.Utils import subst_vars
    ctx.load('find_python')
    ctx.find_python(mandatory=True)
    ctx.declare_runtime_env('PYTHONPATH') 
    pypath = subst_vars('${INSTALL_AREA}/python', ctx.env)
    ctx.env.prepend_value('PYTHONPATH', [pypath])

def build(ctx):
    ctx(features = 'cxx cxxshlib',
        name     = 'cxx-hello-world',
        source   = 'src/mypkgtool.cxx',
        target   = 'hello-world',
        )

    ctx(features     = 'py',
        name         = 'py-hello',
        source       = 'python/pyhello.py python/__init__.py',
        install_path = '${INSTALL_AREA}/python/mypkg',
        use          = 'cxx-hello-world',
        )
    return

`)
	if err != nil {
		t.Fatalf(err.Error())
	}

	ff.Sync()
	ff.Close()

	ff, err = os.Create(filepath.Join(mypkgdir, "src", "mypkgtool.cxx"))
	if err != nil {
		t.Fatalf(err.Error())
	}
	_, err = ff.WriteString(`
#include <cmath>

extern "C" {
  
float
calc_hypot(float x, float y) 
{
  return std::sqrt(x*x + y*y);
}

}

// EOF
`)
	if err != nil {
		t.Fatalf(err.Error())
	}<commit_msg>test: add hscript-based test<commit_after>package main_test

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
)

func TestHwafTuto(t *testing.T) {
	workdir, err := ioutil.TempDir("", "hwaf-test-")
	if err != nil {
		t.Fatalf(err.Error())
	}
	defer os.RemoveAll(workdir)

	err = os.Chdir(workdir)
	if err != nil {
		t.Fatalf(err.Error())
	}

	hwaf, err := newlogger("hwaf.log")
	if err != nil {
		t.Fatalf(err.Error())
	}
	defer hwaf.Close()

	for _, cmd := range [][]string{
		{"hwaf", "init", "-q=0", "."},
		{"hwaf", "setup", "-q=0"},
		{"hwaf", "pkg", "create", "-q=0", "mytools/mypkg"},
		{"hwaf", "pkg", "ls"},
	} {
		err := hwaf.Run(cmd[0], cmd[1:]...)
		if err != nil {
			hwaf.Display()
			t.Fatalf("cmd %v failed: %v", cmd, err)
		}
	}

	mypkgdir := filepath.Join("src", "mytools", "mypkg")

	// create src/mytools/mypkg/wscript file
	ff, err := os.Create(filepath.Join(mypkgdir, "wscript"))
	if err != nil {
		t.Fatalf(err.Error())
	}
	_, err = ff.WriteString(
		`
# -*- python -*-
# automatically generated wscript

import waflib.Logs as msg

PACKAGE = {
    'name': 'mytools/mypkg',
    'author': ["<NAME>"], 
}

def pkg_deps(ctx):
    # put your package dependencies here.
    # e.g.:
    # ctx.use_pkg('AtlasPolicy')
    return

def configure(ctx):
    ctx.load('find_clhep')
    ctx.find_clhep(mandatory=False)
    ctx.start_msg("was clhep found ?")
    ctx.end_msg(ctx.env.HWAF_FOUND_CLHEP)
    if ctx.env.HWAF_FOUND_CLHEP:
        ctx.start_msg("clhep version")
        ctx.end_msg(ctx.env.CLHEP_VERSION)
        msg.info("clhep linkflags: %s" % ctx.env['LINKFLAGS_CLHEP'])
        msg.info("clhep cxxflags: %s" % ctx.env['CXXFLAGS_CLHEP'])
    
    from waflib.Utils import subst_vars
    ctx.load('find_python')
    ctx.find_python(mandatory=True)
    ctx.declare_runtime_env('PYTHONPATH') 
    pypath = subst_vars('${INSTALL_AREA}/python', ctx.env)
    ctx.env.prepend_value('PYTHONPATH', [pypath])

def build(ctx):
    ctx(features = 'cxx cxxshlib',
        name     = 'cxx-hello-world',
        source   = 'src/mypkgtool.cxx',
        target   = 'hello-world',
        )

    ctx(features     = 'py',
        name         = 'py-hello',
        source       = 'python/pyhello.py python/__init__.py',
        install_path = '${INSTALL_AREA}/python/mypkg',
        use          = 'cxx-hello-world',
        )
    return

`)
	if err != nil {
		t.Fatalf(err.Error())
	}

	ff.Sync()
	ff.Close()

	ff, err = os.Create(filepath.Join(mypkgdir, "src", "mypkgtool.cxx"))
	if err != nil {
		t.Fatalf(err.Error())
	}
	_, err = ff.WriteString(`
#include <cmath>

extern "C" {
  
float
calc_hypot(float x, float y) 
{
  return std::sqrt(x*x + y*y);
}

}

// EOF
`)
	if err != nil {
		t.Fatalf(err.Error())
	}

	ff.Sync()
	ff.Close()

	err = os.MkdirAll(filepath.Join(mypkgdir, "python"), 0777)
	if err != nil {
		t.Fatalf(err.Error())
	}

	ff, err = os.Create(filepath.Join(mypkgdir, "python", "__init__.py"))
	if err != nil {
		t.Fatalf(err.Error())
	}
	ff.Close()

	ff, err = os.Create(filepath.Join(mypkgdir, "python", "pyhello.py"))
	if err != nil {
		t.Fatalf(err.Error())
	}

	_, err = ff.WriteString(`
import ctypes
lib = ctypes.cdll.LoadLibrary('libhello-world.so')
if not lib:
    raise RuntimeError("could not find hello-world")

calc_hypot = lib.calc_hypot
calc_hypot.argtypes = [ctypes.c_float]*2
calc_hypot.restype = ctypes.c_float

import sys
sys.stdout.write("hypot(10,20) = %s\n" % calc_hypot(10,20))
sys.stdout.flush()
# EOF #
`)
	if err != nil {
		t.Fatalf(err.Error())
	}
	ff.Sync()
	ff.Close()

	for _, cmd := range [][]string{
		{"hwaf", "configure"},
		{"hwaf"},
		{"hwaf", "run", "python", "-c", "import mypkg.pyhello"},
		{"hwaf", "show", "projects"},
		{"hwaf", "show", "pkg-uses", "mytools/mypkg"},
		{"hwaf", "show", "flags", "CXXFLAGS", "LINKFLAGS"},
		{"hwaf", "show", "constituents"},
		{"hwaf", "pkg", "rm", "mytools/mypkg"},
		{"hwaf", "pkg", "ls"},
	} {
		err := hwaf.Run(cmd[0], cmd[1:]...)
		if err != nil {
			hwaf.Display()
			t.Fatalf("cmd %v failed: %v", cmd, err)
		}
	}
}

func TestHwafTutoHscript(t *testing.T) {
	workdir, err := ioutil.TempDir("", "hwaf-test-")
	if err != nil {
		t.Fatalf(err.Error())
	}
	defer os.RemoveAll(workdir)

	err = os.Chdir(workdir)
	if err != nil {
		t.Fatalf(err.Error())
	}

	hwaf, err := newlogger("hwaf.log")
	if err != nil {
		t.Fatalf(err.Error())
	}
	defer hwaf.Close()

	for _, cmd := range [][]string{
		{"hwaf", "init", "-q=0", "."},
		{"hwaf", "setup", "-q=0"},
		{"hwaf", "pkg", "create", "-q=0", "mytools/mypkg"},
		{"/bin/rm", "-f", "src/mytools/mypkg/wscript"},
		{"hwaf", "pkg", "ls"},
	} {
		err := hwaf.Run(cmd[0], cmd[1:]...)
		if err != nil {
			hwaf.Display()
			t.Fatalf("cmd %v failed: %v", cmd, err)
		}
	}

	mypkgdir := filepath.Join("src", "mytools", "mypkg")

	// create src/mytools/mypkg/wscript file
	ff, err := os.Create(filepath.Join(mypkgdir, "hscript.yml"))
	if err != nil {
		t.Fatalf(err.Error())
	}
	_, err = ff.WriteString(
		`
# -*- yml -*-

package: {
    name: mytools/mypkg,
    authors: "<NAME>",
    deps: {
        public: [],
    },
}

options: {}
configure: {
    tools: [find_clhep, find_python],
    env: {
        PYTHONPATH: "${INSTALL_AREA}/python:${PYTHONPATH}",
    },
}

build: {

    cxx-hello-world: {
        features: "cxx cxxshlib",
        source:   "src/mypkgtool.cxx",
        target:   "hello-world",
    },

    py-hello: {
        features: "py",
        source:   [python/pyhello.py, python/__init__.py],
        install_path: "${INSTALL_AREA}/python/mypkg",
        use:          "cxx-hello-world",
    },
}
`)
	if err != nil {
		t.Fatalf(err.Error())
	}

	ff.Sync()
	ff.Close()

	ff, err = os.Create(filepath.Join(mypkgdir, "src", "mypkgtool.cxx"))
	if err != nil {
		t.Fatalf(err.Error())
	}
	_, err = ff.WriteString(`
#include <cmath>

extern "C" {
  
float
calc_hypot(float x, float y) 
{
  return std::sqrt(x*x + y*y);
}

}

// EOF
`)
	if err != nil {
		t.Fatalf(err.Error())
	}
{'fraction_non_alphanumeric': 0.20340159666782368, 'fraction_numerical': 0.003471017007983339, 'mean_word_length': 2.9724264705882355, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2606288', 'n_tokens_mistral': 3741, 'n_tokens_neox': 3553, 'n_words': 653}