From 3b8b9e0de4b570a3c062e841d69dd2bca8579357 Mon Sep 17 00:00:00 2001 From: Tim Tisdall Date: Fri, 4 Mar 2016 11:33:55 -0500 Subject: [PATCH 1/3] Stop forcing debugging comments when creating sourcemap --- pysass.cpp | 2 +- sass.py | 2 -- sasstests.py | 24 +++++++----------------- 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/pysass.cpp b/pysass.cpp index 7ecdde3a..c8e3995f 100644 --- a/pysass.cpp +++ b/pysass.cpp @@ -575,7 +575,7 @@ PySass_compile_filename(PyObject *self, PyObject *args) { context = sass_make_file_context(filename); options = sass_file_context_get_options(context); - if (source_comments && PySass_Bytes_Check(source_map_filename)) { + if (PySass_Bytes_Check(source_map_filename)) { size_t source_map_file_len = PySass_Bytes_GET_SIZE(source_map_filename); if (source_map_file_len) { char *source_map_file = (char *) malloc(source_map_file_len + 1); diff --git a/sass.py b/sass.py index 23221ad1..7319ad52 100644 --- a/sass.py +++ b/sass.py @@ -537,8 +537,6 @@ def my_importer(path): raise CompileError('source_map_filename is only available with ' 'filename= keyword argument since it has to be ' 'aware of it') - if source_map_filename is not None: - source_comments = True try: include_paths = kwargs.pop('include_paths') or b'' except KeyError: diff --git a/sasstests.py b/sasstests.py index d9ce8ca8..0e215651 100644 --- a/sasstests.py +++ b/sasstests.py @@ -45,10 +45,8 @@ def normalize_path(path): ''' A_EXPECTED_CSS_WITH_MAP = '''\ -/* line 6, SOURCE */ body { background-color: green; } - /* line 8, SOURCE */ body a { color: blue; } @@ -60,8 +58,8 @@ def normalize_path(path): 'sources': ['test/a.scss'], 'names': [], 'mappings': ( - ';AAKA,AAAA,IAAI,CAAC;EAHH,gBAAgB,EAAE,KAAM,GAQzB;;EALD,AAEE,IAFE,' - 'CAEF,CAAC,CAAC;IACA,KAAK,EAAE,IAAK,GACb' + 'AAKA,AAAA,IAAI,CAAC;EAHH,gBAAgB,EAAE,KAAM,GAQzB;EALD,AAEE,' + 'IAFE,CAEF,CAAC,CAAC;IACA,KAAK,EAAE,IAAK,GACb' ), } @@ -71,7 +69,6 @@ def normalize_path(path): ''' B_EXPECTED_CSS_WITH_MAP = '''\ -/* line 2, SOURCE */ b i { font-size: 20px; } @@ -97,10 +94,8 @@ def normalize_path(path): D_EXPECTED_CSS_WITH_MAP = '''\ @charset "UTF-8"; -/* line 6, SOURCE */ body { background-color: green; } - /* line 8, SOURCE */ body a { font: '나눔고딕', sans-serif; } @@ -446,10 +441,7 @@ def test_compile_source_map(self): source_map_filename='a.scss.css.map' ) self.assertEqual( - A_EXPECTED_CSS_WITH_MAP.replace( - 'SOURCE', - normalize_path(os.path.abspath(filename)) - ), + A_EXPECTED_CSS_WITH_MAP, actual ) self.assert_source_map_equal(A_EXPECTED_MAP, source_map) @@ -612,7 +604,7 @@ def replace_source_path(s, name): 'sources': ['../test/b.scss'], 'names': [], 'mappings': ( - ';AAAA,AACE,CADD,CACC,CAAC,CAAC;EACA,SAAS,EAAE,IAAK,' + 'AAAA,AACE,CADD,CACC,CAAC,CAAC;EACA,SAAS,EAAE,IAAK,' 'GACjB' ), }, @@ -632,7 +624,7 @@ def replace_source_path(s, name): 'sources': ['../test/d.scss'], 'names': [], 'mappings': ( - ';;AAKA,AAAA,IAAI,CAAC;EAHH,gBAAgB,EAAE,KAAM,GAQzB;;' + ';AAKA,AAAA,IAAI,CAAC;EAHH,gBAAgB,EAAE,KAAM,GAQzB;' 'EALD,AAEE,IAFE,CAEF,CAAC,CAAC;IACA,IAAI,EAAE,0BAA2B,' 'GAClC' ), @@ -667,7 +659,7 @@ def test_wsgi_sass_middleware(self): self.assertEqual(200, r.status_code) src_path = normalize_path(os.path.join(src_dir, 'a.scss')) self.assert_bytes_equal( - b(A_EXPECTED_CSS_WITH_MAP.replace('SOURCE', src_path)), + b(A_EXPECTED_CSS_WITH_MAP), r.data ) self.assertEqual('text/css', r.mimetype) @@ -818,9 +810,7 @@ def test_sassc_sourcemap(self): self.assertEqual('', self.out.getvalue()) with open(out_filename) as f: self.assertEqual( - A_EXPECTED_CSS_WITH_MAP.replace( - 'SOURCE', normalize_path(src_filename) - ), + A_EXPECTED_CSS_WITH_MAP, f.read().strip() ) with open(out_filename + '.map') as f: From f9fe779968ca3ca9d447e2d79f06148424dcdbde Mon Sep 17 00:00:00 2001 From: Tim Tisdall Date: Fri, 4 Mar 2016 11:46:24 -0500 Subject: [PATCH 2/3] fix flake8 issue --- sasstests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sasstests.py b/sasstests.py index 0e215651..011d732f 100644 --- a/sasstests.py +++ b/sasstests.py @@ -657,7 +657,6 @@ def test_wsgi_sass_middleware(self): self.assertEqual('text/plain', r.mimetype) r = client.get('/static/a.scss.css') self.assertEqual(200, r.status_code) - src_path = normalize_path(os.path.join(src_dir, 'a.scss')) self.assert_bytes_equal( b(A_EXPECTED_CSS_WITH_MAP), r.data From 793bc16c15a810b9d0761272db45ed7f2c351c6f Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 7 Mar 2016 19:33:44 -0800 Subject: [PATCH 3/3] Add --source-comments option to sassc (help, etc. from node-sass) --- sass.py | 7 ++++--- sassc.py | 12 ++++++++++++ sasstests.py | 20 +++++++++++--------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/sass.py b/sass.py index 7319ad52..7f258913 100644 --- a/sass.py +++ b/sass.py @@ -308,8 +308,6 @@ def compile(**kwargs): :param source_map_filename: use source maps and indicate the source map output filename. :const:`None` means not using source maps. :const:`None` by default. - note that it implies ``source_comments`` - is also :const:`True` :type source_map_filename: :class:`str` :param include_paths: an optional list of paths to find ``@import``\ ed SASS/CSS source files @@ -327,7 +325,7 @@ def compile(**kwargs): `_ description :type importers: :class:`collections.Callable` :returns: the compiled CSS string, or a pair of the compiled CSS string - and the source map string if ``source_comments='map'`` + and the source map string if ``source_map_filename`` is set :rtype: :class:`str`, :class:`tuple` :raises sass.CompileError: when it fails for any reason (for example the given SASS has broken syntax) @@ -480,6 +478,9 @@ def my_importer(path): .. versionadded:: 0.7.0 Added ``custom_functions`` parameter. + .. versionadded:: 0.11.0 + ``source_map_filename`` no longer implies ``source_comments``. + """ modes = set() for mode_name in MODES: diff --git a/sassc.py b/sassc.py index eac88f59..0536747f 100755 --- a/sassc.py +++ b/sassc.py @@ -48,6 +48,12 @@ .. versionadded:: 0.7.0 +.. option:: --source-comments + + Include debug info in output. + + .. versionadded:: 0.11.0 + .. option:: -v, --version Prints the program version. @@ -101,6 +107,10 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr): '-p', '--precision', action='store', type='int', default=5, help='Set the precision for numbers. [default: %default]' ) + parser.add_option( + '--source-comments', action='store_true', default=False, + help='Include debug info in output', + ) options, args = parser.parse_args(argv[1:]) error = functools.partial(print, parser.get_prog_name() + ': error:', @@ -134,6 +144,7 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr): css, source_map = compile( filename=filename, output_style=options.style, + source_comments=options.source_comments, source_map_filename=source_map_filename, include_paths=options.include_paths, precision=options.precision @@ -144,6 +155,7 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr): css = compile( filename=filename, output_style=options.style, + source_comments=options.source_comments, include_paths=options.include_paths, precision=options.precision ) diff --git a/sasstests.py b/sasstests.py index 011d732f..3a810e86 100644 --- a/sasstests.py +++ b/sasstests.py @@ -728,7 +728,7 @@ def setUp(self): self.err = StringIO() def test_no_args(self): - exit_code = sassc.main(['sassc', ], self.out, self.err) + exit_code = sassc.main(['sassc'], self.out, self.err) self.assertEqual(2, exit_code) err = self.err.getvalue() assert err.strip().endswith('error: too few arguments'), \ @@ -794,12 +794,11 @@ def test_sassc_source_map_without_css_filename(self): self.assertEqual('', self.out.getvalue()) def test_sassc_sourcemap(self): - tmp_dir = tempfile.mkdtemp() - src_dir = os.path.join(tmp_dir, 'test') - shutil.copytree('test', src_dir) - src_filename = os.path.join(src_dir, 'a.scss') - out_filename = os.path.join(tmp_dir, 'a.scss.css') - try: + with tempdir() as tmp_dir: + src_dir = os.path.join(tmp_dir, 'test') + shutil.copytree('test', src_dir) + src_filename = os.path.join(src_dir, 'a.scss') + out_filename = os.path.join(tmp_dir, 'a.scss.css') exit_code = sassc.main( ['sassc', '-m', src_filename, out_filename], self.out, self.err @@ -817,8 +816,6 @@ def test_sassc_sourcemap(self): dict(A_EXPECTED_MAP, sources=None), dict(json.load(f), sources=None) ) - finally: - shutil.rmtree(tmp_dir) @contextlib.contextmanager @@ -1421,3 +1418,8 @@ def test_stack_trace_formatting(): '>> a{☃\n' ' --^\n\n' ) + + +def test_source_comments(): + out = sass.compile(string='a{color: red}', source_comments=True) + assert out == '/* line 1, stdin */\na {\n color: red; }\n'