From c3304e1ddf82ea319623c20a1d736d0cd743e377 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sun, 12 Jan 2020 20:02:57 +0000 Subject: [PATCH] stdlib: Implement realpath() function. --- docs/Goalfile.pod | 9 +++++++++ stdlib/prelude.gl | 5 +++++ tests/10-function-realpath.gl | 23 +++++++++++++++++++++++ tests/10-function-realpath.sh | 24 ++++++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 tests/10-function-realpath.gl create mode 100755 tests/10-function-realpath.sh diff --git a/docs/Goalfile.pod b/docs/Goalfile.pod index 9917fd0..40ca73a 100644 --- a/docs/Goalfile.pod +++ b/docs/Goalfile.pod @@ -48,6 +48,15 @@ Concatenate C and C. It's not usually necessary to use this function since goals automatically flattens lists within lists into simple lists in many cases. +=head3 realpath (filename) + +For example: + + realpath ("./tests") ⇒ "/home/user/tests" + +Run the L command to return the resolved absolute path of +the C parameter. + =head3 sort (list) For example: diff --git a/stdlib/prelude.gl b/stdlib/prelude.gl index de2a422..183338d 100644 --- a/stdlib/prelude.gl +++ b/stdlib/prelude.gl @@ -90,6 +90,11 @@ pure function extension (name) returning string = @{ echo "${name##*.}" } +# Real path. +function realpath (filename) returning string = @{ + realpath -- %filename +} + # Expand a wildcard into a list of filenames. # # This function is probably not "pure" since it depends on the diff --git a/tests/10-function-realpath.gl b/tests/10-function-realpath.gl new file mode 100644 index 0000000..61aba54 --- /dev/null +++ b/tests/10-function-realpath.gl @@ -0,0 +1,23 @@ +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# Test the realpath function. + +let path = realpath (".") + +goal all = { echo %path } diff --git a/tests/10-function-realpath.sh b/tests/10-function-realpath.sh new file mode 100755 index 0000000..f03b517 --- /dev/null +++ b/tests/10-function-realpath.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +set -e + +goals -f 10-function-realpath.gl > 10-function-realpath.out +test "$(cat 10-function-realpath.out)" = "$PWD" +rm 10-function-realpath.out -- 1.8.3.1